1+ '''
2+ Created on Sep 7, 2017
3+
4+ This program includes the demonstration of
5+ Checkbuttons and Radio Buttons using Tkinter
6+
7+ @author: Aditya
8+ '''
9+
10+ import tkinter as tk
11+ from tkinter import ttk
12+
13+ class Button2App ():
14+ def __init__ (self , master ):
15+
16+ # More advanced as compared to regular buttons
17+ # Check buttons can also store binary values
18+ self .checkbutton = ttk .Checkbutton (master , text = 'Check Me!' )
19+ self .checkbutton .pack ()
20+ self .label = ttk .Label (master , text = 'Ready!! Nothing has happened yet.' )
21+ self .label .pack ()
22+ # Tkinter variable classes
23+ # self.boolvar = tk.BooleanVar() # boolean type variable of tk
24+ # self.dblvar = tk.DoubleVar() # double type variable of tk
25+ # self.intvar = tk.IntVar() # int type variable of tk
26+ self .checkme = tk .StringVar () # string type variable of tk
27+ self .checkme .set ('NULL' ) # set value for string type tkinter variable
28+ print ('Current value of checkme variable is \' {}\' ' .format (self .checkme .get ()))
29+
30+ # setting of binary value for check button: 1. onvaalue and 2. offvalue
31+ self .checkbutton .config (variable = self .checkme , onvalue = 'I am checked!!' , offvalue = 'Waiting for someone to check me!' )
32+ self .checkbutton .config (command = self .oncheckme )
33+
34+
35+ # creating another tkinter string type variable - StringVar
36+ self .papertype = tk .StringVar () # created a variable
37+ self .radiobutton1 = ttk .Radiobutton (master , text = 'Paper1' , variable = self .papertype , value = 'Robotics Research' )
38+ self .radiobutton1 .config (command = self .onselectradio )
39+ self .radiobutton1 .pack ()
40+ self .radiobutton2 = ttk .Radiobutton (master , text = 'Paper2' , variable = self .papertype , value = 'Solid Mechanics Research' )
41+ self .radiobutton2 .config (command = self .onselectradio )
42+ self .radiobutton2 .pack ()
43+ self .radiobutton3 = ttk .Radiobutton (master , text = 'Paper3' , variable = self .papertype , value = 'Biology Research' )
44+ self .radiobutton3 .config (command = self .onselectradio )
45+ self .radiobutton3 .pack ()
46+ self .radiobutton4 = ttk .Radiobutton (master , text = 'Paper4' , variable = self .papertype , value = 'SPAM Research' )
47+ self .radiobutton4 .config (command = self .onselectradio )
48+ self .radiobutton4 .pack ()
49+ self .radiobutton5 = ttk .Radiobutton (master , text = 'Change Checkme text' , variable = self .papertype , value = 'Radio Checkme Selected' )
50+ self .radiobutton5 .pack ()
51+ self .radiobutton5 .config (command = self .onradiobuttonselect )
52+
53+ def oncheckme (self ): # callback function for checkbutton
54+ txt = self .checkme .get () # get the value of stringVar checkme
55+ self .label .config (text = txt ) # display the value of checkme in label
56+
57+ def onradiobuttonselect (self ):
58+ self .checkbutton .config (textvariable = self .papertype )
59+
60+ def onselectradio (self ):
61+ self .label .config (text = self .papertype .get ())
62+
63+ def launchButton2App ():
64+ root = tk .Tk ()
65+ Button2App (root )
66+ tk .mainloop ()
67+
68+ def test ():
69+ launchButton2App ()
70+
71+ if __name__ == '__main__' :
72+ test ()
0 commit comments