我正在尝试学习tkinter,特别是了解grid()如何跨类工作。我是几年前使用pack()从一篇文章中找到的简单代码。要使用grid()而不是pack(),必须对此代码进行哪些更改?我认为这很简单,但是我无法使用grid()创建LabelFrame小部件。
import tkinter as tk
class Testing(tk.LabelFrame):
def __init__(self, parent, main):
self.buttonWidth = 10
self.parent=parent
self.main = main
tk.LabelFrame.__init__(self, self.parent, text="Test Operations",
padx=10,
pady=10
)
self.taskButton = tk.Button(self, text="Do A Task",
width=self.buttonWidth,
command=self.doATask,
)
self.taskButton.pack()
def doATask(self):
self.main.results.getResult()
class Results(tk.LabelFrame):
def __init__(self, parent, main):
self.parent = parent
self.main = main
tk.LabelFrame.__init__(self, self.parent, text="Visual Results")
self.resultLbl = tk.Label(self, text="Result")
self.resultLbl.pack()
def getResult(self):
self.resultLbl.config(bg='yellow')
class Application(tk.Frame):
def __init__(self, parent):
self.parent = parent
tk.Frame.__init__(self, self.parent)
self.testing = Testing(self.parent, self)
self.results = Results(self.parent, self)
self.testing.pack(fill=tk.X)
self.results.pack(fill=tk.X)
if __name__ == "__main__":
root = tk.Tk()
root.title("Modular GUI App")
Application(root).pack()
root.mainloop()