forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__main__.py
More file actions
38 lines (30 loc) · 1.08 KB
/
Copy path__main__.py
File metadata and controls
38 lines (30 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# hello_gui/__main__.py
import tkinter as tk
from tkinter import ttk
try:
from importlib import resources
except ImportError:
import importlib_resources as resources
class Hello(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.wm_title("Hello")
# Read image, store a reference to it, and set it as an icon
with resources.path("hello_gui.gui_resources", "logo.png") as path:
self._icon = tk.PhotoImage(file=path)
self.iconphoto(True, self._icon)
# Read image, create a button, and store a reference to the image
with resources.path("hello_gui.gui_resources", "hand.png") as path:
hand = tk.PhotoImage(file=path)
button = ttk.Button(
self,
image=hand,
text="Goodbye",
command=self.quit,
compound=tk.LEFT, # Add the image to the left of the text
)
button._image = hand
button.pack(side=tk.TOP, padx=10, pady=10)
if __name__ == "__main__":
hello = Hello()
hello.mainloop()