在 Tkinter 应用程序中是否从 Frame 继承 [英] Inheriting from Frame or not in a Tkinter application

查看:25
本文介绍了在 Tkinter 应用程序中是否从 Frame 继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过两种设置 tkinter 程序的基本方法.有什么理由更喜欢一个吗?

I've seen two basic ways of setting up a tkinter program. Is there any reason to prefer one to the other?

from Tkinter import *

class Application():
    def __init__(self, root, title):
        self.root = root
        self.root.title(title) 

        self.label = Label(self.root, text='Hello')
        self.label.grid(row=0, column=0)  

root = Tk()
app = Application(root, 'Sample App')
root.mainloop()

from Tkinter import *

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

        self.label = Label(self, text='Hello')
        self.label.grid(row=0, column=0) 

app = Application('Sample App')
app.mainloop()   

推荐答案

我更喜欢*的选项是从类 Tk 继承.我认为这是更合理的选择,因为窗口实际上是您的应用程序.从 Frame 继承对我来说没有任何意义,然后从 ButtonCanvasLabel 继承.由于您只能拥有一个根,因此您可以继承它是有道理的.

The option I prefer* is to inherit from the class Tk. I think it is the more reasonable choice since the window is, in effect, your application. Inheriting from Frame doesn't make any more sense to me then inheriting from Button or Canvas or Label. Since you can only have a single root, it makes sense that that is what you inherit from.

我还认为,如果您将导入作为 import Tkinter as tk 而不是 from Tkinter import * 进行导入,这会使代码更具可读性.然后,您的所有调用都明确提及 tk 模块.我不建议所有模块都使用它,但对我来说,它对 Tkinter 很有意义.

I also think it makes the code more readable if you do the import as import Tkinter as tk rather than from Tkinter import *. All of your calls then explicitly mention the tk module. I don't recommend this for all modules, but to me it makes sense with Tkinter.

例如:

import Tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.label = tk.Label(text="Hello, world")
        self.label.pack(padx=10, pady=10)

app = SampleApp()
app.mainloop()

* 注意:自从最初写这个答案以来,我改变了我的立场.我现在更喜欢从 Frame 继承而不是从 Tk 继承.无论哪种方式都没有真正的优势,这更像是一种哲学选择.无论如何,我相信无论你是从Frame 还是Tk 继承,我认为这两种选择都比代码中从无继承的第一个例子要好.

* Note: since originally writing this answer I have changed my position. I now prefer to inherit from Frame rather than Tk. There's no real advantage one way or the other, it's more of a philosophical choice than anything else. Regardless, I believe that whether you inherit from Frame or Tk, I think either choice is better than the first example in the code that inherits from nothing.

Frame 继承的一个小优势比 Tk 是在您希望应用程序支持多个相同窗口的情况下.在这种情况下,从 Frame 继承可以让您创建第一个窗口作为根的子窗口,并创建其他窗口作为 Toplevel 实例的子窗口.但是,我见过很少有程序需要这样做.

The one slight advantage inheriting from Frame has over Tk is in the case where you want your application to support multiple identical windows. In that case, inheriting from Frame lets you create the first window as a child of root, and additional windows as children of instances of Toplevel. However, I've seen very few programs that ever have a need to do this.

有关我认为 Tkinter 程序应该如何构建的更多信息,请参阅我对问题"https://stackoverflow.com/q/17466561/7432">Python Tkinter 程序结构.

For more information about how I think Tkinter programs should be structured, see my answer to the question Python Tkinter program structure.

这篇关于在 Tkinter 应用程序中是否从 Frame 继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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