调用 Tk() 实际上有什么作用? [英] What does calling Tk() actually do?

查看:42
本文介绍了调用 Tk() 实际上有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我查看 NMT Tkinter 8.5 参考.

#!/usr/bin/env python
import tkinter as tk

class Application(tk.Frame):    
    def __init__(self, master=None):    
        tk.Frame.__init__(self, master)
        self.grid()
        self.createWidgets()

    def createWidgets(self):    
        self.quitButton = tk.Button(self, text='Quit',command=self.quit)
        self.quitButton.grid()

app = Application()
app.master.title('Sample application')
app.mainloop()

一切都很好,直到我注意到 Tk 类没有被初始化.在我可以找到的其他在线参考资料中(Python 的库参考effbot.orgTkDocs),通常会调用 root = tk.Tk(),其余示例均以此为基础.我也没有在 NMT 的引用中看到任何对 Tk 类初始化的引用.

It's all well and good, until I notice that the Tk class isn't being initialized. In other online reference material I could find (Python's Library Reference, effbot.org, TkDocs), there's usually a call to root = tk.Tk(), from which the rest of the examples are built upon. I also didn't see any sort of reference to the Tk class initialization anywhere on the NMT's reference.

我可以获得的关于 Tk 类的信息也很模糊,Python 参考仅将其列为顶级小部件......通常是应用程序的主窗口".最后,如果我替换之前介绍的代码段中的最后几行:

The information I could get regarding the Tk class is also vague, with the Python Reference only listing it as a "toplevel widget ... which usually is the main window of an application". Lastly, if I replace the last lines in the snippet I presented earlier:

root = tk.Tk()
app = Application(root)

程序会像以前一样运行.考虑到所有这些,我有兴趣了解的是:

The program would run as well as it did before. With all this in mind, what I'm interested in knowing is:

  • 调用 root = tk.Tk() 实际上做了什么(例如,什么被初始化了),为什么没有它,前面的代码片段可以工作?
  • 如果我不调用 Tk() 而只是围绕 Frame 类构建我的应用程序,我会遇到任何陷阱或限制吗?
  • What does calling root = tk.Tk() actually do (as in, what gets initialized) and why can the previous snippet work without it?
  • Would I run into any pitfalls or limitations if I don't call Tk() and just built my application around the Frame class?

推荐答案

Tkinter 的工作原理是在幕后启动 tcl/tk 解释器,然后将 tkinter 命令转换为 tcl/tk 命令.主窗口和这个解释器有着内在的联系,两者都是 tkinter 应用程序工作所必需的.

Tkinter works by starting a tcl/tk interpreter under the covers, and then translating tkinter commands into tcl/tk commands. The main window and this interpreter are intrinsically linked, and both are required for a tkinter application to work.

创建一个 Tk 实例初始化这个解释器并创建根窗口.如果您没有显式初始化它,那么在您创建第一个小部件时将隐式创建一个.

Creating an instance of Tk initializes this interpreter and creates the root window. If you don't explicitly initialize it, one will be implicitly created when you create your first widget.

我认为不自己初始化不会有任何陷阱,但正如 Python 的禅宗所说,显式优于隐式".如果您显式创建 Tk 的实例,您的代码将更容易理解.例如,它会阻止其他人就您的代码提出与您刚刚询问其他代码相同的问题.

I don't think there are any pitfalls by not initializing it yourself, but as the zen of python states, "explicit is better than implicit". Your code will be slightly easier to understand if you explicitly create the instance of Tk. It will, for instance, prevent other people from asking the same question about your code that you just asked about this other code.

这篇关于调用 Tk() 实际上有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆