python编写应用程序步骤_Python-构造tkinter应用程序的最佳方法?

本文提倡使用面向对象的方法来组织Tkinter GUI应用程序的代码,通过创建类来封装窗口、工具栏、导航栏和状态栏,实现代码的模块化和清晰性。作者强调了不使用通配符导入、使用独立类来表示每个窗口和组件的重要性,以减少全局名称空间污染并提高代码可读性。此外,通过共享公共父对象作为控制器,实现了组件之间的简单接口,降低了耦合度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

小编典典

我提倡一种面向对象的方法。这是我开始的模板:

# Use Tkinter for python 2, tkinter for python 3

import tkinter as tk

class MainApplication(tk.Frame):

def __init__(self, parent, *args, **kwargs):

tk.Frame.__init__(self, parent, *args, **kwargs)

self.parent = parent

if __name__ == "__main__":

root = tk.Tk()

MainApplication(root).pack(side="top", fill="both", expand=True)

root.mainloop()

需要注意的重要事项是:

我不使用通配符导入。我将包导入为“ tk”,这要求我在所有命令前添加tk.。这样可以防止全局名称空间污染,并且在使用Tkinter类,ttk类或你自己的某些类时,使代码完全清晰可见。

主要应用是一类。这为你提供了所有回调和私有函数的私有命名空间,并且通常使组织代码更容易。在一种过程样式中,你必须自上而下进行编码,在使用它们之前定义函数等。使用此方法,你无需真正地在最后一步之前创建主窗口。我更喜欢继承,tk.Frame因为我通常从创建框架开始,但这并不是必须的。

如果你的应用还有其他顶级窗口,建议你将每个窗口都设为一个单独的类,并从继承tk.Toplevel。这为你提供了上述所有相同的优点-窗口是原子的,它们具有自己的名称空间,并且代码井井有条。另外,一旦代码开始变大,就可以很容易地将每个模块放入自己的模块中。

最后,你可能要考虑对接口的每个主要部分使用类。例如,如果要创建一个带有工具栏,导航窗格,状态栏和主区域的应用程序,则可以使每个类成为一个类。这使你的主要代码非常小,易于理解:

class Navbar(tk.Frame): ...

class Toolbar(tk.Frame): ...

class Statusbar(tk.Frame): ...

class Main(tk.Frame): ...

class MainApplication(tk.Frame):

def __init__(self, parent, *args, **kwargs):

tk.Frame.__init__(self, parent, *args, **kwargs)

self.statusbar = Statusbar(self, ...)

self.toolbar = Toolbar(self, ...)

self.navbar = Navbar(self, ...)

self.main = Main(self, ...)

self.statusbar.pack(side="bottom", fill="x")

self.toolbar.pack(side="top", fill="x")

self.navbar.pack(side="left", fill="y")

self.main.pack(side="right", fill="both", expand=True)

由于所有这些实例共享一个公共父对象,因此该父对象实际上成为了模型-视图-控制器体系结构的“控制器”部分。因此,例如,主窗口可以通过调用在状态栏上放置一些内容self.parent.statusbar.set(“Hello, world”)。这使你可以在组件之间定义一个简单的接口,从而有助于保持最小的耦合。

2020-02-07

Tkinter is the built-in GUI package that comes with standard python distributions. This means it is easy to get started right away, without any extra installation or configuration. Tkinter’s strength lies in its simplicity of use and its intuitive nature which makes it suited for programmers and non-programmers alike. Once you get started, you will be surprised to see how a few lines of code can produce powerful GUI applications. Tkinter GUI Application Development Hotshot helps you learn the art of GUI programming – building real-world, productive and fun applications like text editor, drum machine, game of chess, media player, drawing application and many more. Each subsequent project builds on the skills acquired in the previous project. Also, learn to write multi-threaded and multi layered applications using Tkinter. Get to know modern best practices involved in writing GUI programs. Tkinter GUI Application Development Hotshot comes with a rich source of sample codes that you can use in your own projects in any discipline of your choice. Starting with a high level overview of Tkinter that covers the most important concepts involved in writing a GUI application, the book then takes you through a series of real world projects of increasing complexity, developing one project per chapter. After you have developed five full projects, the book provides you with some bare-bone skeleton codes for a few functional but incomplete projects, challenging you to put your skills to test by completing them. Finally, you are provided with tips for writing reusable, scalable, and quality GUI code for larger projects. The appendices provide a quick reference sheet for Tkinter. What you will learn from this book Structure your programs in the model-view framework Persist your application data with object serialization Work with external libraries and Tkinter extensions Write multi-threaded GUI programs Re-factor code at every stage of application development Integrate your GUI applications to backend database Use networking with your Tkinter program Apply Internationalization to your GUI applications Develop a GUI program framework for maximum code reuse and rapid application development
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值