如先前所倡导的(Best way to structure a tkinter application),我正在尝试将面向对象的方法应用于tkinter GUI结构。
我正在尝试放置小部件,例如下面的Labelframes中的单选按钮:
import tkinter as tk
root = tk.Tk()
root.title("GUI")
root.geometry('640x480+200+200')
lf0 = tk.LabelFrame(root, text='0 Label Frame', height=100, width=620)
lf0.grid(row=0, column=0, padx=10)
lf1 = tk.LabelFrame(root, text='1 Label Frame', height=100, width=620)
lf1.grid(row=1, column=0, padx=10)
lf2 = tk.LabelFrame(root, text='2 Label Frame')
lf2.grid(row=2, column=0, padx=10)
rb1 = tk.Radiobutton(lf2, text='set')
rb1.grid(padx=287, pady=29, sticky='ew')
root.mainloop()
以下结果导致了类似的GUI布局,但是我无法使用这种方法在标签框架内放置小部件。
import tkinter as tk
class RadioBut(tk.Radiobutton):
def __init__(self, parent, txt, r, c, *args, **kwargs):
# def __init__(self, parent, loc, txt, r, c, *args, **kwargs):
tk.Radiobutton.__init__(self, parent, *args, **kwargs)
self.parent = parent
# self.loc = loc
rb = tk.Radiobutton(parent, text=txt)
# rb = tk.Radiobutton(self.loc, text=txt)
rb.grid(row=r, column=c, sticky='ew')
class LablFrm(tk.LabelFrame):
def __init__(self, parent, txt, r, c, *args, **kwargs):
tk.LabelFrame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.label = tk.LabelFrame(parent, text=txt, height=100, width=620)
self.label.grid(row=r, column=c, padx=10)
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
parent.title("GUI")
parent.geometry('640x480+200+200')
self.widgets()
def widgets(self):
LablFrm(self.parent, '0 Label Frame', 0, 0)
LablFrm(self.parent, '1 Label Frame', 1, 0)
LablFrm(self.parent, '2 Label Frame', 2, 0)
RadioBut(self.parent, 'set', 3, 0)
# lf2 = LablFrm(self.parent, '2 Label Frame', 2, 0)
# rb2 = RadioBut(self.parent, lf2, 'set', 2, 0)
## rb2 = tk.Radiobutton(lf2, text='set')
## rb2.grid(row=0, column=0)
if __name__ == "__main__":
root = tk.Tk()
MainApplication(root)
root.mainloop()
我希望传递一个位置变量,如上面的注释行中所示。甚至连加倍评论的方法都失败了。我想LablFrm定义有些麻烦,我想...
答案 0 :(得分:0)
您将小部件放置在错误的容器(tk.Frames
)中。
我没有检查整个程序,但是经过修改的App类现在将小部件放置在您定制的小部件中:
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
self.parent = parent # save a reference to parent
super().__init__(self.parent, *args, **kwargs) # use super() and place inside parent
# your other widget classes should also be modified this way
self.widgets()
self.pack() # use geometry manager pack on self (tk.Frame subclass)
def widgets(self):
LablFrm(self, '0 Label Frame', 0, 0).grid(row=0, column=0) # place inside self, and apply geometry manager grid
LablFrm(self, '1 Label Frame', 1, 0).grid(row=0, column=1) # same
LablFrm(self, '2 Label Frame', 2, 0).grid(row=0, column=2) # same
RadioBut(self, 'set', 3, 0).grid(row=0, column=3) # same
lf2 = LablFrm(self, '2 Label Frame', r=4, c=0) # same
lf2.grid(row=1, column=0) # apply geometry manager grid
rb2 = RadioBut(self, lf2, text='set', r=4, c=1) # same
rb2.grid(row=1, column=1) # apply geometry manager grid
rb22 = tk.Radiobutton(lf2, text='set') # place inside lf2 widget
rb22.grid(row=2, column=2) # apply geometry manager grid
if __name__ == "__main__":
root = tk.Tk()
root.title("GUI") # avoid modifying a parent class inside a child class
root.geometry('640x480+200+200') # moved these 2 lines out of App
MainApplication(root)
root.mainloop()