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()
运行结果