【发布时间】:2021-07-29 13:55:55
【问题描述】:
我正在尝试使用面向对象的编程风格来编写 Tkinter 应用程序的代码。我想使用类方法将标签(或其他小部件)放置到 GUI。我编写的代码正在向 GUI 添加一个我不希望的字符。如何编写初始 add_label 方法 使其不添加不需要的字符。 下面是我的代码和截图。我是 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()
【问题讨论】:
-
你能举例说明你想要什么和你得到什么吗?
-
@CoolCloud 添加截图
-
删除
self.add_label(root)。