我想制作GUI,我想将GUI分成几个类。 (注意:我对编程很新)。在主类中,我将调用实例(GUI的一部分)并使用网格来创建完整的GUI。问题是我认为在我的主类中应该决定放置实例的位置,但只显示最后一个实例。
import tkinter as tk
import tkinter.ttk as ttk
class Code(tk.Frame):
def __init__(self, parent, *args):
tk.Frame.__init__(self, parent)
self.frm = ttk.LabelFrame(parent, text="frm_code", height=130, width=500)
# make grid with just one cell - I thought this is local grid placement
self.frm.grid(row=0, column=0, sticky=(tk.N, tk.S, tk.W, tk.E))
self.frm.grid_propagate(False)
self.mess_sql = tk.Message(
self.frm,
text="""Veeeery long text""",
width = 470
)
self.mess_sql.grid(row=0, column=0, sticky=(tk.N, tk.S, tk.W, tk.E))
class Header(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.frm = ttk.LabelFrame(parent, text="frm_header")
# make grid with just one cell - I thought this is local grid placement
self.frm.grid(row=0, column=0, sticky=(tk.N, tk.S, tk.W, tk.E))
self.btn_ok = ttk.Button(self.frm, text="OK", command=self.click_ok)
self.btn_cancel= ttk.Button(self.frm, text="Cancel", command=self.click_cancel)
self.btn_ok.grid(row=0, column=0)
self.btn_cancel.grid(row=0, column=1)
# just some test functions
def click_ok(self):
print("OK clicked")
def click_cancel(self):
print("Cancel clicked")
class App(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.header = Header(parent)
self.code = Code(parent)
# this should place widgets, but for now does nothing
self.header.grid(row=0, column=0)
self.header.grid(row=1, column=0)
if __name__ == '__main__':
root = tk.Tk()
app = App(root)
root.mainloop()