您的位置:首页 > 编程语言 > Python开发

Python Tkinter 图形组件介绍

2020-07-21 04:12 1236 查看

1、 窗口  Tkinter.Tk() 

# -*- coding: UTF-8 -*-
import Tkinter

myWindow = Tkinter.Tk()
myWindow.title('南风丶轻语')
myWindow.geometry('400x500')
myWindow.mainloop()

  运行结果

 

2、 标签 Label   Tkinter.Label()

# -*- coding: UTF-8 -*-
import Tkinter

myWindow = Tkinter.Tk()
myWindow.title('南风丶轻语')
myWindow.geometry('400x500')

textVar = Tkinter.StringVar()
textVar.set('我是一个标签')
mylabel = Tkinter.Label(myWindow, bg='yellow', width=20, height=1,textvariable=textVar)
mylabel.pack()

myWindow.mainloop()

  运行结果

 

3、 按钮 Tkinter.Button()

# -*- coding: UTF-8 -*-
import Tkinter

myWindow = Tkinter.Tk()
myWindow.title('南风丶轻语')
myWindow.geometry('400x500')

textVar = Tkinter.StringVar()
textVar.set('我是一个标签')
mylabel = Tkinter.Label(myWindow, bg='yellow', width=20, height=1,textvariable=textVar)
mylabel.pack()

print type(textVar.get())

clickFlag = False
def clickEvet():
global clickFlag
if clickFlag == False:
textVar.set('我是被点击后的标签')
clickFlag = True
else:
textVar.set('我是一个标签')
clickFlag = False

myButton = Tkinter.Button(myWindow, width=10, height=1,text='点击', command=clickEvet)
myButton.pack()

myWindow.mainloop()

  运行结果

 

4、 输入框 Tkinter.Entry()

  备注 : 程序功能为点击按钮,标签显示 entry 中输入的内容

# -*- coding: UTF-8 -*-
import Tkinter

myWindow = Tkinter.Tk()
myWindow.title('南风丶轻语')
myWindow.geometry('400x500')

textVar = Tkinter.StringVar()
textVar.set('我是一个标签')
mylabel = Tkinter.Label(myWindow, bg='yellow', width=20, height=1,textvariable=textVar)
mylabel.pack()

def clickEvet():
textVar.set(myEntry.get())

myButton = Tkinter.Button(myWindow, width=10, height=1,text='点击', command=clickEvet)
myButton.pack()

myEntry = Tkinter.Entry(myWindow, width=20,)
myEntry.pack()

myWindow.mainloop()

  运行结果

 

5、 文本框

  备注 : 实现功能为点击按钮,文本框插入数据(插入位置可以是鼠标位置,也可以是末尾,我们用两个按钮控制实现该效果)

# -*- coding: UTF-8 -*-
import Tkinter

myWindow = Tkinter.Tk()
myWindow.title('南风丶轻语')
myWindow.geometry('400x500')

myText = Tkinter.Text(myWindow, height=5, show=None)
myText.pack()

def clickEvet1():
myText.insert('insert', '鼠标位置插入数据')

def clickEvet2():
myText.insert('end', '末尾插入数据')

myButton = Tkinter.Button(myWindow, width=10, height=1,text='鼠标插入', command=clickEvet1)
myButton.pack()

myButton2 = Tkinter.Button(myWindow, width=10, height=1,text='末尾插入', command=clickEvet2)
myButton2.pack()

myWindow.mainloop()

  运行结果

 

6、 listBox

  备注 : 点击按钮,实现文本框显示选中的 listBox 的内容

# -*- coding: UTF-8 -*-
import Tkinter

myWindow = Tkinter.Tk()
myWindow.title('南风丶轻语')
myWindow.geometry('400x500')

myText = Tkinter.Text(myWindow, height=5, show=None)
myText.pack()

def clickEvet():
myText.insert('end', myListBox.get(myListBox.curselection()))

myButton2 = Tkinter.Button(myWindow, width = 10, height = 1,text = '点击', command = clickEvet)
myButton2.pack()

listBoxVar = Tkinter.StringVar()
listBoxVar.set((11, 22, 33, 'tan'))
myListBox = Tkinter.Listbox(myWindow, listvariable = listBoxVar)
myListBox.pack()

myWindow.mainloop()

  运行结果

 

7、CheckBox

  备注:实现选中 CheckBox, 标签中显示选中内容

# -*- coding: UTF-8 -*-
import Tkinter

myWindow = Tkinter.Tk()
myWindow.title('CheckButton Test Window')
myWindow.geometry('430x430')

labelText = Tkinter.StringVar()
lable = Tkinter.Label(myWindow, textvariable=labelText, bg='yellow', width=30, height=1)
lable.pack(side='top')

def chooseEvent():
if (chooseFirst.get() == 1) and (chooseSecond.get() == 0) and (chooseThird.get() == 0):
labelText.set('Tan')
elif (chooseFirst.get() == 0) and (chooseSecond.get() == 1) and (chooseThird.get() == 0):
labelText.set('Xiao')
elif (chooseFirst.get() == 0) and (chooseSecond.get() == 0) and (chooseThird.get() == 1):
labelText.set('Hui')
elif (chooseFirst.get() == 1) and (chooseSecond.get() == 1) and (chooseThird.get() == 0):
labelText.set('TanXiao')
elif (chooseFirst.get() == 1) and (chooseSecond.get() == 0) and (chooseThird.get() == 1):
labelText.set('TanHui')
elif (chooseFirst.get() == 0) and (chooseSecond.get() == 1) and (chooseThird.get() == 1):
labelText.set('XiaoHui')
elif (chooseFirst.get() == 1) and (chooseSecond.get() == 1) and (chooseThird.get() == 1):
labelText.set('TanXiaoHui')
else:
labelText.set('')

chooseFirst = Tkinter.IntVar()
checkButton1 = Tkinter.Checkbutton(myWindow, text='Tan', variable=chooseFirst, onvalue=1, offvalue=0, command=chooseEvent)
checkButton1.place(x=200, y=22)
chooseSecond = Tkinter.IntVar()
checkButton2 = Tkinter.Checkbutton(myWindow, text='Xiao', variable=chooseSecond, onvalue=1, offvalue=0, command=chooseEvent)
checkButton2.place(x=200, y=44)
chooseThird = Tkinter.IntVar()
checkButton3 = Tkinter.Checkbutton(myWindow, text='Hui', variable=chooseThird, onvalue=1, offvalue=0, command=chooseEvent)
checkButton3.place(x=200, y=66)

myWindow.mainloop()

  运行结果

 

转载于:https://www.cnblogs.com/rainbow-tan/p/11366079.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: