官术网_书友最值得收藏!

How to do it...

To improve the modularity of our simple program, we will define a class that wraps our global variables:

import tkinter as tk 
 
class App(tk.Tk): 
    def __init__(self): 
        super().__init__() 
        self.btn = tk.Button(self, text="Click me!", 
                             command=self.say_hello) 
        self.btn.pack(padx=120, pady=30) 
 
    def say_hello(self): 
        print("Hello, Tkinter!") 
 
if __name__ == "__main__": 
    app = App() 
    app.title("My Tkinter app") 
    app.mainloop()

Now, each variable is enclosed in a specific scope, including the command function, which is moved as a separate method.

主站蜘蛛池模板: 佳木斯市| 城固县| 东城区| 麻城市| 青铜峡市| 永昌县| 青河县| 乌兰县| 蓝田县| 和田市| 纳雍县| 临朐县| 长垣县| 含山县| 承德县| 广饶县| 囊谦县| 勃利县| 彰武县| 巴林左旗| 布尔津县| 达尔| 叙永县| 若尔盖县| 蓬溪县| 深州市| 乳源| 临颍县| 温宿县| 调兵山市| 宜兴市| 鹤峰县| 略阳县| 元朗区| 曲周县| 赫章县| 陕西省| 阿拉善右旗| 孝感市| 江西省| 岳阳县|