Skip to content

Commit 721dcfe

Browse files
committed
Modify simple_gif image, add demo for button widget
1 parent ffc85a7 commit 721dcfe

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

src/main.py

+2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import program1 as p1
1010
import program2 as p2
1111
import program3 as p3
12+
import program4 as p4
1213

1314
def main():
1415
p1.sayhello()
1516
p2.HelloAppLaunch()
1617
p3.GreetingAppLaunch()
18+
p4.launchButtonApp()
1719

1820
if __name__ == '__main__':main()

src/program4.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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()

src/simple_gif.gif

-13.6 KB
Loading

0 commit comments

Comments
 (0)