Advertisement
here2share

# Tk_RadioButtons.py

Oct 20th, 2016
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | None | 0 0
  1. # Tk_RadioButtons.py
  2.  
  3. from Tkinter import *
  4. import random
  5. import time
  6.  
  7. root = Tk()
  8. root.resizable(0,0)
  9. root.geometry("300x100")
  10.  
  11. canvas = Canvas(root)
  12. canvas.pack()
  13.  
  14. var1=IntVar()
  15. var2=IntVar()
  16.  
  17. abc='ABC'
  18. def sABC():
  19.    selection="You have selected Option " + str(abc[var1.get()])
  20.    oABC.config(text=selection)
  21.  
  22. def s123():
  23.    selection="You have selected Option " + str(var2.get())
  24.    x123.config(text=selection)
  25.    var1.set(-1)  # will deselect option sABC instead of var1.deselect()
  26. #
  27. OptionBtn1=Radiobutton(canvas, text="Option A", variable=var1, value=0, command=sABC)
  28. OptionBtn1.grid(row=0, column=1)
  29.  
  30. OptionBtn2=Radiobutton(canvas, text="Option B", variable=var1, value=1, command=sABC)
  31. OptionBtn2.grid(row=0, column=2)
  32.  
  33. OptionBtn3=Radiobutton(canvas, text="Option C", variable=var1, value=2, command=sABC)
  34. OptionBtn3.grid(row=0, column=3)
  35.  
  36. OptionBtn1=Radiobutton(canvas, text="Option 1", variable=var2, value=1, command=s123)
  37. OptionBtn1.grid(row=1, column=1)
  38.  
  39. OptionBtn2=Radiobutton(canvas, text="Option 2", variable=var2, value=2, command=s123)
  40. OptionBtn2.grid(row=1, column=2)
  41.  
  42. OptionBtn3=Radiobutton(canvas, text="Option 3", variable=var2, value=3, command=s123)
  43. OptionBtn3.grid(row=1, column=3)
  44.  
  45. var1.set(2) # sets selection on 'C' for option sABC as default
  46.  
  47. oABC=Label(canvas, text="Note: 'C' has been set as default")
  48. oABC.grid(columnspan=4, sticky=W)
  49.  
  50. x123=Label(canvas, text="None Selected")
  51. x123.grid(columnspan=4, sticky=W)
  52.  
  53. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement