Skip to content

Commit e5a5d83

Browse files
committed
Bug fix in use of Image with Label widget
1 parent 9ea31eb commit e5a5d83

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/program3.py

+14-15
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,33 @@ def __init__(self, master): # constructor method
2424

2525
# store image in the label obj to keep it in the scope as
2626
# long as label is displayed and to avoid getting the image getting garbage collected
27-
imgpath = 'simple_gif.gif'
28-
self.label.img = tk.PhotoImage(file = imgpath)
29-
self.label.config(compound = 'left') # to display image in left of text
30-
27+
imgpath = 'simple_gif.gif' # path of the image
28+
self.label.img = tk.PhotoImage(file = imgpath) # read_image :: saving image within label_object to prevent its garbage collection
29+
self.label.config(image = self.label.img) # display image in label widget
30+
self.label.config(compound = 'left') # to display image in left of text
3131

3232
self.label.pack() # pack label to the window with pack() geometry method of Label
3333
self.btn.pack()
34-
self.label.config(wraplength = 200)
35-
self.label.config(justify = tk.CENTER) # justify can be CENTER, RIGHT, LEFT
34+
35+
self.label.config(wraplength = 200) # specify wraplength of text in label
36+
self.label.config(justify = tk.CENTER) # justify text in label to (CENTER, RIGHT or LEFT)
3637
self.label.config(foreground = 'blue', background = 'yellow') # insert font color (foreground) and background color
3738
self.label.config(font = ('Times', 10, 'bold')) # font = (font_name, font_size, font_type)
3839

39-
# store image in the label obj to keep it in the scope as
40-
# long as label is displayed and to avoid getting the image getting garbage collected
41-
imgpath = 'simple_gif.gif'
42-
self.label.img = tk.PhotoImage(file = imgpath)
43-
self.label.config(image = self.label.img)
44-
self.label.config(compound = 'left') # to display image in left of text
45-
4640
def handle_text(self):
4741
if self.label['text'] == self.greet_list[0]:
4842
self.label.config(text = self.greet_list[1])
4943
else:
5044
self.label.config(text = self.greet_list[0])
51-
52-
def test():
45+
46+
47+
def GreetingAppLaunch(): # function to use class defined
5348
root = tk.Tk()
5449
GreetingApp(root)
5550
root.mainloop()
51+
52+
53+
def test():
54+
GreetingAppLaunch()
5655

5756
if __name__ == '__main__': test()

0 commit comments

Comments
 (0)