示例:
1 #用户界面 2 import os 3 os.chdir('F:\spyder_workspace\ColCal') 4 import Main 5 from tkinter import * 6 from tkinter import ttk 7 8 root = Tk() 9 root.title("window") 10 root.geometry('500x500') 11 #创建标签 12 var1 = StringVar() 13 l = Label(root, bg='green', fg='yellow',font=('Arial', 12), width=10, textvariable=var1) 14 l.pack() 15 #列表框点击事件 16 def print_lb1(): 17 value = lb1.get(lb1.curselection()) 18 var1.set(value) 19 #列表框点击按钮 20 b1 = Button(root,text='print selection lb',width=18,height=2,command=print_lb1) 21 b1.pack() 22 #创建Listbox 23 var_lb1 = StringVar() 24 var_lb1.set(('C30','C35','C40')) 25 lb1 = Listbox(root,listvariable = var_lb1) 26 lb1.pack() 27 28 #组合框点击事件 29 def print_cb1(): 30 value = cb1.get() 31 var1.set(value) 32 #组合框点击按钮 33 b2 = Button(root,text='print selection cb',width=18,height=2,command=print_cb1) 34 b2.pack() 35 #创建Combobox 36 var_cb1 = StringVar() 37 var_cb1.set('请选择混凝土标号') 38 cb1 = ttk.Combobox(root,textvariable=var_cb1) 39 cb1['values']=['C30','C35','C40'] 40 cb1.pack() 41 #事件循环 42 root.mainloop()