tkinter tutorial 1: encapsulate gui with classes and threads
This article blog link: http://blog.csdn.net/jdh99 , Author: jdh, reprint please specify.
surroundings:
python version: 3.5
Development environment: pyCharm
Description:
Python commonly used GUIs include tkinter and pyqt. The advantage of tkinter is that the language is built-in, easy to learn, and suitable for making gadgets that don't care about the aesthetics of the interface. This series of articles introduces the commonly used controls of tkinter.
The gui is best encapsulated in a thread and separated from the business code. This article provides a sample program as a reference.
The layout of tkinter is recommended to be grid-based, and auxiliary to pack.
Bale:
Use pyinstaller to package the program into an exe.
command:
pyinstaller -F test.py
If you increase the parameter -w, the terminal window will be hidden
If there are multiple py files in the project, you can import the involved py files in the packaged py file.
Source code:
import tkinter as tk
import threading
def main():
threading.Thread(target=gui_thread).start()
def gui_thread():
root = tk.Tk()
app = App(root)
root.mainloop()
class App:
def __init__(self, root):
frame = tk.Frame(root)
frame.pack()
self.button = tk.Button(frame, text='Hello', command=self.say_hi)
self.button.pack(side=tk.LEFT)
@staticmethod
def say_hi():
print('Hello World!')
if __name__ == '__main__':
main()
operation result: