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

How it works...

First, we replaced the wildcard import with the import ... as syntax to have better control over our global namespace.

Then, we defined our App class as a Tk subclass, which now is referenced via the tk namespace. To properly initialize the base class, we will call the __init__ method of the Tk class with the built-in super() function. This corresponds to the following lines:

class App(tk.Tk): 
    def __init__(self): 
        super().__init__() 
        # ... 

Now, we have a reference to the App instance with the self variable, so we will add all the Button widget as an attribute of our class.

Although it may look overkill for such a simple program, this refactoring will help us to reason about each part, the button instantiation is separated from the callback that gets executed when it is clicked, and the application bootstrapping is moved to the if __name__ == "__main__" block, which is a common practice in executable Python scripts.

We will follow this convention through all the code samples, so you can take this template as the starting point of any larger application.

主站蜘蛛池模板: 成都市| 白朗县| 贵阳市| 任丘市| 通州区| 边坝县| 龙泉市| 金门县| 禄丰县| 灵璧县| 图木舒克市| 浮山县| 榆林市| 上高县| 清涧县| 荆州市| 临夏市| 浦城县| 昂仁县| 岫岩| 辉县市| 屏东市| 长兴县| 阿克陶县| 平顶山市| 沅陵县| 普兰县| 天祝| 鸡西市| 浏阳市| 南木林县| 望奎县| 开平市| 莫力| 和田县| 循化| 玉山县| 邹城市| 格尔木市| 紫阳县| 许昌县|