探索tkinter控件
探索Label控件
添加一个Label控件到窗口中,可以使用text参数来定义要显示的文本
self.label1 = Label(self,text = 'This is a test label')
添加Button控件
基本格式为:
self.button1 = Button(self,text = '',command = )
使用Checkbutton控件
使用Checkbutton控件之前,我们需要创建表示复选框的状态的特殊变量,这称为控制变量。
我们有以下四种方法创建一个控制变量:
- BooleanVar() --布尔值为0和1
- DoubleVar() --浮点值
- IntVar --整数值
- StringVar() --文本值
self.varCheck1 = BooleanVar()#返回一个布尔值
self.check1 = Checkbutton(self,text = 'Option1',
variable = self.varCheck1)
要检索Checkbutton控件的状态,我们需要使用控制变量的get()方法。
option1 = self.varCheck1.get()
if (option1):
print('The checkbutton was selected')
else:
print('The checkbutton was not selected')
使用Entry控件
创建一个Entry控件:
self.entry1 = Entry(self)
使用Entry控件在Python窗口中输入和输出:
from tkinter import *
class Application(Frame):
def __init__(self,master):
super(Application,self).__init__(master)
self.grid()
self.creat_widgets()
def creat_widgets(self):
self.label1 = Label(self,text = 'Please enter some text in lower case')
self.label1.grid(row = 0)
self.text1 = Entry(self)
self.text1.grid(row = 2)
self.button1 = Button(self,text = 'Convert text',command = self.convert)
self.button1.grid(row = 6,column = 0)
self.button2 = Button(self,text = 'Clear result',command = self.clear)
self.button2.grid(row = 6,column = 1)
self.text1.focus_set()
def convert(self):
varText = self.text1.get()
varReplaced = varText.upper()
self.text1.delete(0,END)
self.text1.insert(END,varReplaced)
def clear(self):
self.text1.delete(0,END)
self.focus_set()
root = Tk()
root.title('Testing and Entry text')
root.geometry('500x200')
app = Application(root)
app.mainloop()
运行结果
添加Text控件
创建基本格式为:
self.text1 = Text(self,options)
我们常用width和height两个选项来控制Text控件在窗口中的大小(width定义字符长度,height定义行数)。
如同Entry控件,我们可以使用delete()方法删除文本内容,使用insert()方法向控件添加文本,使用get()方法获取Text控件中的文本,但是,这些方法指定的索引值不是一个数值。
实际上是由两部分组成的文本值:‘x.y’
x是行的位置(从1开始),y是列的位置(从0开始),当我们要引用Text控件的第一个字符,可以使用索引值‘1.0’。
下面是Text控件的基本用法:
from tkinter import *
class Application(Frame):
def __init__(self,master):
super(Application,self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.label1 = Label(self,text = 'Enter the text to convert:')
self.label1.grid(row = 0,column = 0,sticky = W)
self.text1 = Text(self,width = 20,height = 10)
self.text1.grid(row = 1,column = 0)
self.text1.focus_set()
self.button1 = Button(self,text = 'Covert',
command = self.convert)
self.button1.grid(row = 2,column = 0)
self.button2 = Button(self,text = 'clear',
command = self.clear)
self.button2.grid(row = 2,column = 1)
def convert(self):
varText = self.text1.get('1.0',END)
varReplaced = varText.upper()
self.text1.delete('1.0',END)
self.text1.insert(END,varReplaced)
def clear(self):
self.text1.delete('1.0',END)
self.text1.focus_set()
root = Tk()
root.title('Text widget test')
app = Application(root)
app.mainloop()
运行结果
使用ListBox控件
创建的基本格式为:
self.listbox1 = Listbox(self,selectmode = )
参数selectmode有下面几种可用的选项:
- SINGLE – 一次只能选择一个项目
- BROWSE – 一次只能选择一个,但是可以从列表中移除项目
- MULTIPLE – 可以通过单击选择多个项目
- EXTENDED – 可以在单击时使用Shift和Ctrl键选择多个项目
下面展示如何使用Listbox控件。
from tkinter import *
class Application(Frame):
def __init__(self,master):
super(Application,self).__init__(master)
self.grid()
self.creat_widgets()
def creat_widgets(self):
self.label1 = Label(self,text = 'Select your items')
self.label1.grid(row = 0)
self.listbox1 = Listbox(self,selectmode = EXTENDED)
items = ['item One','item Two','item Three']
for item in items:
self.listbox1.insert(END,item)
self.listbox1.grid(row = 1)
self.button1 = Button(self,text = 'submit',command = 'self.display')
self.button1.grid(row = 2)
def display(self):
items = self.listbox1.curselection()#转化为元组
for item in items:
Stritem = self.listbox1.get(item)
print(Stritem)
print('-------')
root = Tk()
root.title('Listbox widget test')
root.geometry('500x200')
app = Application(root)
app.mainloop()
运行结果
使用Menu控件
一个GUI程序的重要部分是在窗口顶部的菜单栏,菜单栏提供了下拉菜单,使程序用户可以快速做出选择。
#把Menu控件链接到Frame对象上
menubar = Menu(self)
filemenu = Menu(menubar)
#使用add_command方法添加一个菜单栏
filemenu.add_command(label = 'Convert',command = self.convert)
filemenu.add_command(label = 'Clear',command = self.clear)
#使用add_cascade()方法添加到最高级的菜单栏
menubar.add_cascade(label = 'File',menu = filemenu)
menubar.add_command(label = 'Quit',command = root.quit)
#将菜单栏链接到根对象Tk上
root.config(menu = rself.menuba)
下面,我们练习编写一个真实的程序测试一下Menu控件:
from tkinter import *
class Application(Frame):
def __init__(self,master):
super(Application,self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
menubar = Menu(self)
filemenu = Menu(menubar)
filemenu.add_command(label = 'Calculate',command = 'self.calculate')
filemenu.add_command(label = 'Reset',command = self.clear)
menubar.add_cascade(label = 'File',menu = filemenu)
menubar.add_command(label = 'Quit',command = 'root.quit')
self.label1 = Label(self,text = 'The Bowling Calculator')
self.label1.grid(row = 0,columnspan = 3)
self.label2 = Label(self,text = 'Enter score from game 1')
self.label3 = Label(self,text = 'Enter score from game 2')
self.label4= Label(self,text = 'Enter score from game 3')
self.label5= Label(self,text = 'Average')
self.label2.grid(row = 2,column = 0)
self.label3.grid(row = 3,column = 0)
self.label4.grid(row = 4,column = 0)
self.label5.grid(row = 5,column = 0)
self.score1 = Entry(self)
self.score2 = Entry(self)
self.score3 = Entry(self)
self.average = Entry(self)
self.score1.grid(row = 2,column = 1)
self.score2.grid(row = 3,column = 1)
self.score3.grid(row = 4,column = 1)
self.average.grid(row = 5,column = 1)
self.button1 = Button(self,text = 'Calculate Average',
command = self.calculate)
self.button1.grid(row = 6,column = 1)
self.button2 = Button(self,text = 'Clear result',
command = self.clear)
self.button2.grid(row = 6,column = 2)
self.score1.focus_set()
root.config(menu = menubar)
def calculate(self):
numScore1 = int(self.score1.get())
numScore2 = int(self.score2.get())
numScore3 = int(self.score3.get())
total = numScore1 + numScore2 + numScore3
average = total / 3
strAverage ="{0:.2f}".format(average)
self.average.insert(0,strAverage)
def clear(self):
self.score1.delete(0,END)
self.score2.delete(0,END)
self.score3.delete(0,END)
self.average.delete(0,END)
self.score1.focus_set()
root = Tk()
root.title('Bowling Average Calculate')
root.geometry('500x200')
app = Application(root)
app.mainloop()
运行结果
总的来说,GUI的编程让计算机和用户能够更多的交互,让python的世界更加精彩。
转载:https://blog.csdn.net/mantouyouyou/article/details/105235285