欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

tkinter的常见组件应用

程序员文章站 2022-05-15 18:05:27
...

一 代码

  1. import tkinter
  2. import tkinter.messagebox
  3. import tkinter.ttk
  4. #创建tkinter应用程序
  5. root = tkinter.Tk()
  6. #设置窗口标题
  7. root.title('Selection widgets')
  8. #定义窗口大小
  9. root['height']=400
  10. root['width']=320
  11. #与姓名关联的变量
  12. varName = tkinter.StringVar()
  13. varName.set('')
  14. #创建标签,然后放到窗口上
  15. labelName = tkinter.Label(root, text='Name:',justify=tkinter.RIGHT,width=50)
  16. labelName.place(x=10, y=5, width=50, height=20)
  17. #创建文本框,同时设置关联的变量
  18. entryName = tkinter.Entry(root, width=120,textvariable=varName)
  19. entryName.place(x=70, y=5, width=120, height=20)
  20. labelGrade = tkinter.Label(root, text='Grade:', justify=tkinter.RIGHT, width=50)
  21. labelGrade.place(x=10, y=40, width=50, height=20)
  22. #模拟学生所在年级,字典键为年级,字典值为班级
  23. studentClasses ={'1':['1','2','3','4'],
  24. '2':['1','2'],
  25. '3':['1','2','3']}
  26. #学生年级组合框
  27. comboGrade = tkinter.ttk.Combobox(root,width=50,
  28. values=tuple(studentClasses.keys()))
  29. comboGrade.place(x=70, y=40, width=50, height=20)
  30. #事件处理函数
  31. def comboChange(event):
  32. grade = comboGrade.get()
  33. if grade:
  34. #动态改变组合框可选项
  35. comboClass["values"]= studentClasses.get(grade)
  36. else:
  37. comboClass.set([])
  38. #绑定组合框事件处理函数
  39. comboGrade.bind('<<ComboboxSelected>>', comboChange)
  40. labelClass = tkinter.Label(root, text='Class:', justify=tkinter.RIGHT, width=50)
  41. labelClass.place(x=130, y=40, width=50, height=20)
  42. #学生年级组合框
  43. comboClass = tkinter.ttk.Combobox(root, width=50)
  44. comboClass.place(x=190, y=40, width=50, height=20)
  45. labelSex = tkinter.Label(root, text='Sex:', justify=tkinter.RIGHT, width=50)
  46. labelSex.place(x=10, y=70, width=50, height=20)
  47. #与性别关联的变量,1:男;0:女,默认为男
  48. sex = tkinter.IntVar()
  49. sex.set(1)
  50. #单选钮,男
  51. radioMan = tkinter.Radiobutton(root,variable=sex,value=1,text='Man')
  52. radioMan.place(x=70, y=70, width=50, height=20)
  53. #单选钮,女
  54. radioWoman = tkinter.Radiobutton(root,variable=sex,value=0,text='Woman')
  55. radioWoman.place(x=130, y=70, width=70, height=20)
  56. #与是否班长关联的变量,默认当前学生不是班长
  57. monitor = tkinter.IntVar()
  58. monitor.set(0)
  59. #复选框,选中时变量值为1,#未选中时变量值为0
  60. checkMonitor = tkinter.Checkbutton(root,text='Is Monitor?', variable=monitor,
  61. onvalue=1, offvalue=0)
  62. checkMonitor.place(x=20, y=100, width=100, height=20)
  63. #添加按钮单击事件处理函数
  64. def addInformation():
  65. result ='Name:'+ entryName.get()
  66. result = result +';Grade:'+ comboGrade.get()
  67. result = result +';Class:'+ comboClass.get()
  68. result = result +';Sex:'+('Man'if sex.get()else'Woman')
  69. result = result +';Monitor:'+('Yes'if monitor.get()else'No')
  70. listboxStudents.insert(0, result)
  71. buttonAdd = tkinter.Button(root, text='Add',width=40, command=addInformation)
  72. buttonAdd.place(x=130, y=100, width=40, height=20)
  73. #删除按钮的事件处理函数
  74. def deleteSelection():
  75. selection = listboxStudents.curselection()
  76. ifnot selection:
  77. tkinter.messagebox.showinfo(title='Information', message='No Selection')
  78. else:
  79. listboxStudents.delete(selection)
  80. buttonDelete = tkinter.Button(root, text='DeleteSelection',
  81. width=100, command=deleteSelection)
  82. buttonDelete.place(x=180, y=100, width=100, height=20)
  83. #创建列表框组件
  84. listboxStudents = tkinter.Listbox(root, width=300)
  85. listboxStudents.place(x=10, y=130, width=300, height=200)
  86. #启动消息循环
  87. root.mainloop()
二 运行结果
tkinter的常见组件应用
            
    
    博客分类: python tkinter组件 
 
  • tkinter的常见组件应用
            
    
    博客分类: python tkinter组件 
  • 大小: 8.9 KB
相关标签: tkinter 组件