Skip to main content
 首页 » 编程设计

python之Main 和 Class 中的 Grid 和 Pack

2024年10月25日9unruledboy

我刚刚将我在 Tkinter 中的布局从包移动到网格。这进展顺利。我在 Main 函数中错误地同时使用了 pack 和 grid。我修复它,而不仅仅是使用抓地力。但我意识到我仍在构建 Tkinter 小部件的类中使用 pack。这些都是标签。看起来这没问题并且有效,但我认为这不是一个值得遵循的好习惯。如果我在类中使用网格,那么它会在 Main 的网格中嵌入自己的网格吗?当在类中创建一些小部件而在 Main 中创建一些小部件时,布局屏幕的最佳方式是什么?

所以这是仍然在里面的类....

class StopWatch(Frame): 
    ... 
 
    def makeWidgets(self): 
        """ Make the time labels. """ 
        la = Label(self, textvariable=self.timestr) 
        la.pack(fill=X, expand=NO, pady=2, padx=2) 
 
        lb = Label(self, textvariable=self.timestr) 
        lb.pack(fill=X, expand=NO, pady=2, padx=2) 
 
        lc = Label(self, textvariable=self.timestr) 
        lc.pack(fill=X, expand=NO, pady=2, padx=2) 
 
        ld = Label(self, textvariable=self.timestr) 
        ld.pack(fill=X, expand=NO, pady=2, padx=2) 
 
        lsplita = Label(self, textvariable=self.lapastr) 
        lsplita.pack(fill=X, expand=NO, pady=2, padx=2) 
 
        lsplitb = Label(self, textvariable=self.lapbstr) 
        lsplitb.pack(fill=X, expand=NO, pady=2, padx=2) 
 
        lsplitc = Label(self, textvariable=self.lapcstr) 
        lsplitc.pack(fill=X, expand=NO, pady=2, padx=2) 
 
        lsplitd = Label(self, textvariable=self.lapdstr) 
        lsplitd.pack(fill=X, expand=NO, pady=2, padx=2) 
 
        self._setTime(self._elapsedtime) 

现在这是我的 Main 网格....

def main(): 
    root = Tk() 
    root.geometry("500x400") 
    sw1 = StopWatch(root) 
    sw1.grid(row=0, column=5, rowspan=2) 
 
    b1 = Button(root, text='Start', command=sw1.Start) 
    b2 = Button(root, text='Stop', command=sw1.Stop) 
    b3 = Button(root, text='Reset', command=sw1.Reset) 
    b4 = Button(root, text='Quit', command=root.quit) 
    b5 = Button(root, text='Get Split A', command=sw1.Getsplita) 
    b6 = Button(root, text='Get Split B', command=sw1.Getsplitb) 
    b7 = Button(root, text='Get Split C', command=sw1.Getsplitc) 
    b8 = Button(root, text='Get Split D', command=sw1.Getsplitd) 
    b1.grid(row=0, column=0) 
    b2.grid(row=0, column=1) 
    b3.grid(row=0, column=2) 
    b4.grid(row=0, column=3) 
    b5.grid(row=1, column=0) 
    b6.grid(row=1, column=1) 
    b7.grid(row=1, column=2) 
    b8.grid(row=1, column=3) 
 
    root.mainloop() 

请您参考如下方法:

最佳做法是在应用程序中同时grid 和pack。

唯一需要注意的是,您不能在给定的框架内混合使用它们。框架中小部件的几何管理器(grid、pack、place)的选择完全独立于任何其他框架中使用的几何管理器.每个都有长处和短处,您应该相应地使用它们。

另外——我经常看到这个错误——从小部件继承的类应该不调用grid或pack或 place 自身。

例如,这是不正确:

class Example(tk.Frame): 
    def __init__(self, parent): 
        ... 
        self.pack(...) 
 
class Main(tk.Frame): 
    def __init__(self, parent): 
        ... 
        this.example = Example(self) 

代替上面的,pack应该从Example中移除,并且Main应该重新定义来调用pack(或 grid 或 place):

class Main(tk.Frame): 
    def __init__(self, parent): 
        ... 
        this.example = Example(self) 
        this.example.pack(...) 

第一个(不正确的)示例的问题是,如果您在 main 中有十几个框架以及一些其他小部件,如果您决定从 pack 切换到 grid,你将不得不修改其他十几个类。通过让父级负责安排自己的子级,当您更改布局时,您只需更改一个功能。