简体   繁体   English

使用 class 方法创建 TKinter label

[英]Create TKinter label using class method

I am trying use object oriented programming style to write the code for a Tkinter app.我正在尝试使用面向 object 的编程风格来编写 Tkinter 应用程序的代码。 I want to use a class method to place labels(or other widgets) to the GUI.我想使用 class 方法将标签(或其他小部件)放置到 GUI。 The code I wrote is adding a character which I don't expect to the GUI.我编写的代码正在向 GUI 添加一个我不希望的字符。 How can I write the initial add_label method so that it does not add the unwanted character.如何编写初始add_label 方法,使其不添加不需要的字符。 Below is my code and a screenshot.下面是我的代码和截图。 I am new to OOP, so i might be missing something.我是 OOP 的新手,所以我可能会遗漏一些东西。

from tkinter import *
class App:
    def __init__(self, parent):
        self.widgets(root)
        self.add_label(root)

    def widgets(self, app):
        self.title = Label(app, text= 'LABEL UP').pack()
        self.btn = Button(app, text = 'BUTTON').pack()
    def add_label(self, text):
        Label(text= text).pack()

root = Tk()
App(root)
App.add_label(root, 'LABEL_1')
App.add_label(root,'LABEL_2')
root.mainloop()

在此处输入图像描述 I am new to OOP and stil trying to figure out how i can benefit from code reuse in this case.我是 OOP 的新手,并且仍在试图弄清楚在这种情况下如何从代码重用中受益。 My app has several widgets and functions我的应用程序有几个小部件和功能

What do you expect self.add_label(root) to do?你期望self.add_label(root)做什么? According to your method definition, it takes text as argument, so when you say self.add_label(root) , you are passing root as text .根据您的方法定义,它将text作为参数,因此当您说self.add_label(root)时,您将root作为text传递。 And what is root ?什么是root It is '.'它是'.' , so remove it and it'll be gone. ,所以删除它,它就会消失。

Though a proper way to do this will be to pass a parent argument to the method and use that while widget creation:虽然这样做的正确方法是将parent参数传递给方法并在创建小部件时使用它:

And the important part is, your instantiating the class wrong.重要的是,您实例化class错误。 Keep a reference to it, rather than creating a lot of instances.保留对它的引用,而不是创建很多实例。

from tkinter import *

class App:
    def __init__(self, parent):
        self.widgets(root)

    def widgets(self, app):
        self.title = Label(app, text= 'LABEL UP').pack()
        self.btn = Button(app, text = 'BUTTON').pack()
    
    def add_label(self, parent, text):
        Label(parent,text= text).pack()

root = Tk()

app = App(root)
app.add_label(root, 'LABEL_1')
app.add_label(root,'LABEL_2')

root.mainloop()

Try not to get confused with both the mistakes.尽量不要混淆这两个错误。

How would I write this class?我将如何写这个 class? I don't know the true purpose of this, but I think you can follow something like this:我不知道这样做的真正目的,但我认为您可以遵循以下内容:

from tkinter import *

class App:
    def __init__(self, parent):
        self.parent = parent
        
        self.title = Label(self.parent, text='LABEL UP')
        self.title.pack()

        self.entry = Entry(self.parent)
        self.entry.pack()

        self.btn = Button(self.parent, text='BUTTON')
        # Compliance with PEP8
        self.btn.config(command=lambda: self.add_label(self.entry.get())) 
        self.btn.pack()

    def add_label(self, text):
        Label(self.parent, text=text).pack()

    def start(self):
        self.parent.mainloop()

root = Tk()

app = App(root)
app.start()

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

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