/Chapter01/Ch01_Code/GUI_radiobutton_widget.py

https://github.com/PacktPublishing/Python-GUI-Programming-Cookbook-Second-Edition
Python | 101 lines | 57 code | 20 blank | 24 comment | 7 complexity | 3a0d0026776f4621f982a6bec44af970 MD5 | raw file
  1. '''
  2. May 2017
  3. @author: Burkhard A. Meier
  4. '''
  5. #======================
  6. # imports
  7. #======================
  8. import tkinter as tk
  9. from tkinter import ttk
  10. # Create instance
  11. win = tk.Tk()
  12. # Add a title
  13. win.title("Python GUI")
  14. # Modify adding a Label
  15. a_label = ttk.Label(win, text="A Label")
  16. a_label.grid(column=0, row=0)
  17. # Modified Button Click Function
  18. def click_me():
  19. action.configure(text='Hello ' + name.get() + ' ' +
  20. number_chosen.get())
  21. # Changing our Label
  22. ttk.Label(win, text="Enter a name:").grid(column=0, row=0)
  23. # Adding a Textbox Entry widget
  24. name = tk.StringVar()
  25. name_entered = ttk.Entry(win, width=12, textvariable=name)
  26. name_entered.grid(column=0, row=1)
  27. # Adding a Button
  28. action = ttk.Button(win, text="Click Me!", command=click_me)
  29. action.grid(column=2, row=1) # <= change column to 2
  30. # Creating three checkbuttons
  31. ttk.Label(win, text="Choose a number:").grid(column=1, row=0)
  32. number = tk.StringVar()
  33. number_chosen = ttk.Combobox(win, width=12, textvariable=number, state='readonly')
  34. number_chosen['values'] = (1, 2, 4, 42, 100)
  35. number_chosen.grid(column=1, row=1)
  36. number_chosen.current(0)
  37. chVarDis = tk.IntVar()
  38. check1 = tk.Checkbutton(win, text="Disabled", variable=chVarDis, state='disabled')
  39. check1.select()
  40. check1.grid(column=0, row=4, sticky=tk.W)
  41. chVarUn = tk.IntVar()
  42. check2 = tk.Checkbutton(win, text="UnChecked", variable=chVarUn)
  43. check2.deselect()
  44. check2.grid(column=1, row=4, sticky=tk.W)
  45. chVarEn = tk.IntVar()
  46. check3 = tk.Checkbutton(win, text="Enabled", variable=chVarEn)
  47. check3.deselect()
  48. check3.grid(column=2, row=4, sticky=tk.W)
  49. # GUI Callback function
  50. def checkCallback(*ignoredArgs):
  51. # only enable one checkbutton
  52. if chVarUn.get(): check3.configure(state='disabled')
  53. else: check3.configure(state='normal')
  54. if chVarEn.get(): check2.configure(state='disabled')
  55. else: check2.configure(state='normal')
  56. # trace the state of the two checkbuttons
  57. chVarUn.trace('w', lambda unused0, unused1, unused2 : checkCallback())
  58. chVarEn.trace('w', lambda unused0, unused1, unused2 : checkCallback())
  59. # Radiobutton Globals
  60. COLOR1 = "Blue"
  61. COLOR2 = "Gold"
  62. COLOR3 = "Red"
  63. # Radiobutton Callback
  64. def radCall():
  65. radSel=radVar.get()
  66. if radSel == 1: win.configure(background=COLOR1)
  67. elif radSel == 2: win.configure(background=COLOR2)
  68. elif radSel == 3: win.configure(background=COLOR3)
  69. # create three Radiobuttons using one variable
  70. radVar = tk.IntVar()
  71. rad1 = tk.Radiobutton(win, text=COLOR1, variable=radVar, value=1, command=radCall)
  72. rad1.grid(column=0, row=5, sticky=tk.W, columnspan=3)
  73. rad2 = tk.Radiobutton(win, text=COLOR2, variable=radVar, value=2, command=radCall)
  74. rad2.grid(column=1, row=5, sticky=tk.W, columnspan=3)
  75. rad3 = tk.Radiobutton(win, text=COLOR3, variable=radVar, value=3, command=radCall)
  76. rad3.grid(column=2, row=5, sticky=tk.W, columnspan=3)
  77. name_entered.focus() # Place cursor into name Entry
  78. #======================
  79. # Start GUI
  80. #======================
  81. win.mainloop()