1
+ '''
2
+ Created on Aug 20, 2017
3
+
4
+ @author: Aditya
5
+
6
+ This program demonstrates the use of button widget in tkinter
7
+ '''
8
+ import tkinter as tk
9
+ from tkinter import ttk
10
+
11
+ class ButtonApp :
12
+ def __init__ (self , master ):
13
+ self .button = ttk .Button (master , text = 'Click me' )
14
+ self .button .pack ()
15
+ self .button .config (command = self .buttonfunc ) # configure a command for button click
16
+
17
+ self .btn1 = ttk .Button (master , text = 'Click on \' Click me\' ' , command = self .invokebutton )
18
+ self .btn1 .pack ()
19
+
20
+ self .btn2 = ttk .Button (master , text = 'Disable \' Click me\' ' , command = self .disableButton )
21
+ self .btn2 .pack ()
22
+
23
+ self .btn3 = ttk .Button (master , text = 'Enable \' Click me\' ' , command = self .enableButton )
24
+ self .btn3 .pack ()
25
+
26
+ self .btn4 = ttk .Button (master , text = 'Query state of \' Click me\' ' , command = self .queryButtonState )
27
+ self .btn4 .pack ()
28
+
29
+ self .button .img = tk .PhotoImage (file = 'simple_gif.gif' )
30
+ self .button .img = self .button .img .subsample (10 , 10 ) # take every 5th pixel in x and y direction of image
31
+
32
+ self .btn5 = ttk .Button (master , text = 'Add image to \' Click me\' ' , command = self .addImage )
33
+ self .btn5 .pack ()
34
+
35
+ self .label = ttk .Label (master , text = 'No button pressed yet.' )
36
+ self .label .pack ()
37
+
38
+ def buttonfunc (self ):
39
+ self .label .config (text = 'Clicked!!' )
40
+
41
+ def invokebutton (self ):
42
+ self .button .invoke () # invoke the button
43
+
44
+ def disableButton (self ):
45
+ if self .button .instate (['disabled' ]):
46
+ self .label .config (text = '\' Click me\' is already disabled.' )
47
+ else :
48
+ self .button .state (['disabled' ]) # returns the previous state of button and sets the new state
49
+ self .label .config (text = '\' Click me\' is disabled.' )
50
+
51
+ def queryButtonState (self ):
52
+ if self .button .instate (['disabled' ]):
53
+ self .label .config (text = '\' Click me\' is in diabled state.' )
54
+ else :
55
+ self .label .config (text = '\' Click me\' is in enabled state.' )
56
+
57
+ def enableButton (self ):
58
+ if self .button .instate (['!disabled' ]):
59
+ self .label .config (text = '\' Click me\' is already enabled' )
60
+ else :
61
+ self .button .state (['!disabled' ])
62
+ self .label .config (text = '\' Click me\' is enabled' )
63
+
64
+ def addImage (self ):
65
+ self .button .config (image = self .button .img , compound = tk .LEFT )
66
+
67
+ def launchButtonApp ():
68
+ root = tk .Tk () # instantiate the main window class by creating its object
69
+ ButtonApp (root )
70
+ tk .mainloop ()
71
+
72
+ def test ():
73
+ launchButtonApp ()
74
+
75
+ if __name__ == '__main__' : test ()
0 commit comments