1. 概述
1.1 案例说明
本案例是一个简易的学生管理操作界面,通过本案例的练习来熟悉单选、复选、列表以及组合框的使用。
该学生管理主要进行学生记录添加、删除操作。如下面图片所示:
image.png
2. 单选、复选、列表以及组合框的基本使用
同样,磨刀不误砍柴工,在进行案例代码编写前,先花点时间完成下面4个代码段的练习,熟悉这些组件的使用方法。
2.1 Listbox 列表框
import tkinter
import tkinter.messagebox
window = tkinter.Tk()
window.title('my window')
window.geometry('200x200')
var = tkinter.StringVar()
# 为变量设置值
var.set((2020, 2021, 2022))
# 创建Listbox,将var的值赋给Listbox,可以显示8行记录
lb = tkinter.Listbox(window, listvariable=var, height=8)
# 创建一个list并将值循环添加到Listbox控件中
list_items = ['1-watermelon', '2-strawberry', '3-grape']
for item in list_items:
# 从最后一个位置开始加入值
lb.insert('end', item)
# 在第一个位置加入'first'字符
lb.insert(1, 'first')
# 在第二个位置加入'second'字符
lb.insert(2, 'second')
# 删除第二个位置的字符
lb.delete(2)
lb.pack()
# 显示已经被选择的list记录
def show_selection():
value = lb.get(lb.curselection()) # 获取当前选中的文本
tkinter.messagebox.showinfo(title='my message', message=value)
b1 = tkinter.Button(window, text='show selection', width=15, height=2, command=show_selection)
b1.pack()
window.mainloop()
运行结果如下:
listbox.png
2.2 Radiobutton单选按钮
import tkinter
import tkinter.messagebox
window = tkinter.Tk()
window.title('my window')
window.geometry('400x200')
var = tkinter.StringVar()
var.set('A')
l = tkinter.Label(window, text='你最不喜欢的一个编程语言是哪一项?')
l.pack()
def show_selected():
value = var.get() # 获取当前选中项
tkinter.messagebox.showinfo(title='my message', message=value)
r1 = tkinter.Radiobutton(window, text='A.C语言 ',
variable=var, value='A',
command=show_selected)
r1.pack()
r2 = tkinter.Radiobutton(window, text='B.Java语言 ',
variable=var, value='B',
command=show_selected)
r2.pack()
r3 = tkinter.Radiobutton(window, text='C.Python语言 ',
variable=var, value='C',
command=show_selected)
r3.pack()
r3 = tkinter.Radiobutton(window, text='D.以上都不喜欢',
variable=var, value='D',
command=show_selected)
r3.pack()
window.mainloop()
运行结果如下:
image.png
2.3. Checkbutton 复选按钮
import tkinter
import tkinter.messagebox
window = tkinter.Tk()
window.title('my window')
window.geometry('400x200')
var1 = tkinter.StringVar()
var1.set('-')
var2 = tkinter.StringVar()
var2.set('-')
var3 = tkinter.StringVar()
var3.set('-')
var4 = tkinter.StringVar()
var4.set('-')
l = tkinter.Label(window, text='你喜欢下面哪些编程语言?')
l.pack()
def show_selected():
value1 = var1.get() # 获取当前选中项
value2 = var2.get()
value3 = var3.get()
value4 = var4.get()
s = ''
if value1 == '1':
s += ' C '
if value2 == '2':
s += ' Java '
if value3 == '3':
s += ' Python '
if value4 == '4':
s += ' R '
if s == '':
s = '我不喜欢编程!!!'
else:
s = '我喜欢'+s+'!!!'
tkinter.messagebox.showinfo(title='my message', message=s)
cb1 = tkinter.Checkbutton(window, text='A.C语言 ',
variable=var1, onvalue=1, offvalue=0,
command=show_selected)
cb1.pack()
cb2 = tkinter.Checkbutton(window, text='B.Java语言 ',
variable=var2, onvalue=2, offvalue=0,
command=show_selected)
cb2.pack()
cb3 = tkinter.Checkbutton(window, text='C.Python语言 ',
variable=var3, onvalue=3, offvalue=0,
command=show_selected)
cb3.pack()
cb4 = tkinter.Checkbutton(window, text='D.R语言 ',
variable=var4, onvalue=4, offvalue=0,
command=show_selected)
cb4.pack()
window.mainloop()
运行结果如下:
Checkbutton .png
2.4. Combobox组合框
import tkinter
import tkinter.messagebox
import tkinter.ttk
window = tkinter.Tk()
window.title('my window')
window.geometry('400x200')
labelGrade = tkinter.Label(window, text='Grade:', justify=tkinter.RIGHT, width=50)
labelGrade.place(x=10, y=40, width=50, height=20)
# 模拟学生所在班级,字典键为年级,字典值为班级
studentClasses = {'1': ['1', '2', '3', '4'], '2': ['1', '2'], '3': ['1', '2', '3']}
# 学生年级组合框
comboGrade = tkinter.ttk.Combobox(window, values=tuple(studentClasses.keys()), width=50)
comboGrade.place(x=70, y=40, width=50, height=20)
# 事件处理函数
def comboChange(event):
grade = comboGrade.get()
if grade:
# 动态改变组合框可选项
comboClass["values"] = studentClasses.get(grade)
else:
comboClass.set([])
# 绑定事件处理函数
comboGrade.bind('<<ComboboxSelected>> ', comboChange)
labelClass = tkinter.Label(window, text='Class:', justify=tkinter.RIGHT,width=50)
labelClass.place(x=130, y=40, width=50, height=20)
comboClass = tkinter.ttk.Combobox(window, width=50)
# 学生年级组合框
comboClass.place(x=190, y=40, width=50, height=20)
window.mainloop()
运行结果如下:
image.png
3. 案例代码实现
import tkinter
import tkinter.messagebox
import tkinter.ttk
root = tkinter.Tk()
root.title('stu manager')
root['height'] = 400
root['width'] = 400
labelName = tkinter.Label(root, text='Name:', justify=tkinter.RIGHT, width=50)
labelName.place(x=10, y=5, width=50, height=20)
varName = tkinter.StringVar(value='')
entryName = tkinter.Entry(root, width=120, textvariable=varName)
entryName.place(x=70, y=5, width=120, height=20)
labelGrade = tkinter.Label(root, text='Grade:', justify=tkinter.RIGHT, width=50)
labelGrade.place(x=10, y=40, width=50, height=20)
# 模拟学生所在班级,字典键为年级,字典值为班级
studentClasses = {'1': ['1', '2', '3', '4'], '2': ['1', '2'], '3': ['1', '2', '3']}
# 学生年级组合框
comboGrade = tkinter.ttk.Combobox(root, values=tuple(studentClasses.keys()), width=50)
comboGrade.place(x=70, y=40, width=50, height=20)
# 事件处理函数
def comboChange(event):
grade = comboGrade.get()
if grade:
# 动态改变组合框可选项
comboClass["values"] = studentClasses.get(grade)
else:
comboClass.set([])
# 绑定事件处理函数
comboGrade.bind('<<ComboboxSelected>> ', comboChange)
labelClass = tkinter.Label(root, text='Class:', justify=tkinter.RIGHT,width=50)
labelClass.place(x=130, y=40, width=50, height=20)
comboClass = tkinter.ttk.Combobox(root, width=50)
# 学生年级组合框
comboClass.place(x=190, y=40, width=50, height=20)
labelSex = tkinter.Label(root, text='Sex:', justify=tkinter .RIGHT, width=50)
labelSex.place(x=10, y=70, width=50, height=20)
# 与性别关联的变量,1:男;0:女,默认为男
sex = tkinter.IntVar(value=1)
# 单选钮,男
radioMan = tkinter.Radiobutton(root, variable=sex, value=1, text='Man')
radioMan.place(x=70, y=70, width=50, height=20)
radioWoman = tkinter. Radiobutton(root, variable=sex, value=0, text='Woman')
radioWoman.place(x=130, y=70, width=70, height=20)
# 与是否班长关联的变量,默认不是班长
monitor = tkinter.IntVar(value=0)
# 单选框,选中时变量值为1,未选中时变量值为0
checkMonitor = tkinter.Checkbutton(root, text='IS Monitor?', variable=monitor, onvalue=1, offvalue=0)
checkMonitor .place(x=20, y=100, width=100, height=20)
def addInformation():
result = 'Name:' + entryName.get()
result = result + ';Grade:' + comboGrade .get()
result = result + ';Class:' + comboClass .get()
result = result + ';sex:' + ('Man' if sex.get() else 'Woman')
result = result + ';Monitor:' + ('Yes' if monitor.get() else 'No')
listboxStudents.insert(0, result)
buttonAdd = tkinter.Button(root, text='Add', width=40, command= addInformation)
buttonAdd.place(x=130, y=100, width=40, height=20)
def deleteSelection():
selection = listboxStudents .curselection()
if not selection:
tkinter.messagebox.showinfo(title='Information', message='No Selection')
else:
listboxStudents. delete(selection)
buttonDelete = tkinter .Button(root, text='DeleteSelection', width=100, command=deleteSelection)
buttonDelete.place(x=180, y=100, width=100, height=20)
listboxStudents = tkinter .Listbox(root, width=350)
# 创建列表
listboxStudents.place(x=10, y=130, width=300, height=200)
root.mainloop()
网友评论