简体   繁体   English

Python tkinter 标签

[英]Python tkinter Label

Hi I have a little question about Label in tkinter.嗨,我有一个关于 tkinter 中的标签的小问题。

When you use Label outside classes, you do something like当您在类外使用 Label 时,您会执行类似的操作

import tkinter as tk


root = tk.Tk()
label = tk.Label(root, text = "something", background = "something")
label.pack()

However, when it's inside a class and the code goes something like但是,当它在类中并且代码类似于

import tkinter as tk将 tkinter 作为 tk 导入

class Example(tk.Frame):

    COLOURS = [ "#f45", "#ee5", "#aa4", "#a1e433", "#e34412", "#116611",
                "#111 eeefff", "#3aa922191", "#abbabbaaa" ]

    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.parent = parent


        col = 1
        for colour in Example.COLOURS:
        #
        label = tk.Label(self, text=colour, background=colour)
        #
        label.grid(row=1, column=col)
        col += 1


def main():
root = tk.Tk()
ex = Example(root)
root.geometry("+300+300")
root.mainloop()

if __name__ == '__main__':
    main()

but shouldn't it be rather like但不应该更像

label = tk.Label(self.parent, text=colour, background=colour)

since self.parent would correspond to root?因为 self.parent 会对应于 root? When I try to do that, I get an error and I only do when I have the label.grid(...) line under it(I tried pack and it worked fine).当我尝试这样做时,我收到一个错误,并且只有当我在它下面有 label.grid(...) 行时才会这样做(我试过 pack 并且它工作正常)。

So I thought this code所以我认为这段代码

import tkinter as tk

root = tk.Tk()

label = tk.Label(root)
label.grid(row=0, column=0)

root.mainloop()

wouldn't work either, but it actually worked fine.也不会工作,但它实际上工作得很好。 So I'm confused.所以我很困惑。 Can anyone explain?谁能解释一下?

No, it should not be self.parent .不,它不应该是self.parent

In the class example you give, the class is itself a frame.在您给出的类示例中,类本身就是一个框架。 It is designed this way to make the example self-contained.以这种方式设计使示例自包含。 By inheriting from Frame you can take all of the code in that class and put it anywhere in the GUI.通过从Frame继承,您可以获取该类中的所有代码并将其放在 GUI 中的任何位置。 You can think of the class and everything in it as a single custom widget.您可以将类及其中的所有内容视为单个自定义小部件。 You could have multiple of these classes, and each one can be treated as a single GUI object.您可以拥有多个这样的类,并且每个类都可以被视为一个 GUI 对象。

To make that work, the class only ever puts widgets inside itself, not in its parent.为了实现这一点,该类只将小部件放在自身内部,而不是在其父类中。

The entire purpose of using a sublcass of Frame is to act as a container for other widgets.使用Frame的子类的全部目的是充当其他小部件的容器。 If you don't plan on using it as a container for other widgets, there's no point in inheriting from Frame .如果您不打算将其用作其他小部件的容器,那么从Frame继承就没有意义了。

It is the equivalent of this, without classes:它相当于这个,没有类:

import tkinter as tk

root = tk.Tk()
frame = tk.Frame(root)
frame.pack(...)
label = tk.Label(frame, text = "something", background = "something")
label.pack(...)

If you wanted the class to put widgets in the parent, you would define the class like the following.如果您希望类将小部件放在父类中,您可以像下面这样定义类。 Notice that it inherits from object rather than Frame :请注意,它继承自object而不是Frame

class Example(object):
    def __init__(self, parent):
        self.parent = parent
        ...
        label = tk.Label(parent, ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM