我对python非常陌生,目前正在尝试以稍微不同的方式组织我的tkinter应用程序。我试图使用类来使应用程序更加模块化,并且能够在应用程序中的多个位置使用类中的方法。这里是我有的更新的代码:Python,使用tkinter如何自定义UI组件的类显示?
from tkinter import *
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
self.QUIT.pack(side=LEFT)
self.hi_there.pack(side=RIGHT)
Frame.__init__(self, master)
self.pack()
self.createAnotherWidget()
self.title_label.pack(side=LEFT)
self.title_entry.pack(side=RIGHT)
def say_hi(self):
print("hi there, everyone!")
def createWidgets(self):
self.QUIT = Button(self)
self.QUIT["text"] = "QUIT"
self.QUIT["fg"] = "red"
self.QUIT["command"] = self.quit
self.hi_there = Button(self)
self.hi_there["text"] = "Hello",
self.hi_there["command"] = self.say_hi
def createAnotherWidget(self):
self.title_label = Label(self)
self.title_label["text"] = "Title: "
self.title_entry = Entry(self)
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
self.createAnotherWidget()
root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()
这运行没有错误,但没有在窗口中显示。我如何定制这些方法的代码在渲染时将放置在哪里?对于这个例子,我只是想的createAnotherWidget
在createWidgets
如果你正在做任何潜在的大规模的tkinter应用程序(有多个框架或具有超过4个小工具),我会研究.grid函数的工作原理。如果你把它用于你的整个程序,包装是非常混乱的,以便跟踪。祝你好运 –