Tkinter Class Demo
A very simple example of using Tkinter with classes
The root window
1class WindowOne(tk.Tk):
2 """
3 This is the root window of a tkinter demo done with classes and using the place()
4 method to put widgets in precise x/y coordinates.
5 Args:
6 (tk.Tk): Inheriting from tk.Tk so this can become the "root" window (the app)
7 """
8 def __init__(self):
9 super().__init__()
10 self.title('Window One')
11 self.geometry('640x480')
12 # Things outside of this class do not need access to these variables
13 self._is_down_clk: bool = False
14 self._DOWN_BTN_PATH: str = 'down_button.png'
15 self._DOWN_BTN_TXT: str = 'Download'
16 self._UP_BTN_TXT: str = 'Upload'
17 self._UP_BTN_PATH: str = 'up_button.png'
18 self._down_btn_png: tk.PhotoImage = tk.PhotoImage(file=self._DOWN_BTN_PATH)
19 self._up_btn_png: tk.PhotoImage = tk.PhotoImage(file=self._UP_BTN_PATH)
20 self._hello_label: tk.Label = tk.Label(
21 self,
22 text="Hello, Tkinter",
23 foreground="white", # Set the text color to white
24 background="black" # Set the background color to black
25 )
26 self._btn_img: tk.Button = tk.Button(self,
27 text=self._DOWN_BTN_TXT,
28 image=self._down_btn_png,
29 command=lambda: self.img_btn_on_click())
30 self._hello_label.place(x=5, y=5)
31 self.update()
32 self._btn_img.place(x=self._hello_label.winfo_width()+5, y=5)
33 self._btn_new_window: tk.Button = tk.Button(
34 self,
35 text='open window',
36 command=lambda: self.btn_new_window_on_click())
37 self.update()
38 self._btn_new_window.place(x=0, y=self._btn_img.winfo_height()+10)
39
40 def btn_new_window_on_click(self) -> None:
41 """
42 Create the second window and set focus to it
43 """
44 new_window: WindowTwo = WindowTwo(self)
45 new_window.grab_set() # force this window to the foreground
46
47 def img_btn_on_click(self) -> None:
48 """
49 Respond to the img_btn "OnClick" event and demonstrate changing
50 an image within the button at runtime
51 """
52 if self._is_down_clk:
53 self._is_down_clk = False
54 self._btn_img.config(text = self._UP_BTN_TXT, image = self._up_btn_png)
55 else:
56 self._is_down_clk = True
57 self._btn_img.config(text = self._DOWN_BTN_TXT, image = self._down_btn_png)
Class WindowTwo
1class WindowTwo(tk.Toplevel):
2 """
3 This creates our second window for some data entry
4
5 Args:
6 (tk.Toplevel): We inherit from the tk.Toplevel class
7 """
8 def __init__(self, parent: tk.Tk):
9 super().__init__(parent)
10 self.title('Second Window')
11 self.geometry('320x240')
12 # Things outside of this class do not need access to these variables
13 self._data_entry: tk.Entry = tk.Entry(self, width=50, bg='blue', fg='yellow')
14 self._data_entry.place(x=0, y=0)
15 self._btn_get_data: tk.Button = tk.Button(self, text='Get Data', command=lambda: self.get_data())
16 self._btn_close: tk.Button = tk.Button(self, text='Close', command=lambda: self.destroy())
17 self.update() # we need to inform TK to draw what it has so we can get the control measurements
18 self._btn_get_data.place(x=0, y=self._data_entry.winfo_height()+5)
19 self.update() # we need to inform TK to draw what it has so we can get the control measurements
20 self._btn_close.place(x=self._btn_get_data.winfo_width()+10, y=self._btn_get_data.winfo_y())
21
22 def get_data(self) -> None:
23 """
24 Get the data from the data_entry widget
25 """
26 print(self._data_entry.get())
Execute the Code
1window_one = WindowOne()
2window_one.mainloop()