Tkinter-是否可以在同一帧中使用两个不同的网格?

时间:2020-10-01 00:15:10

标签: tkinter grid

我写了一个简单的代码:

function roll(theRoll){
    diceSet = theRoll;

    console.log("before")
    console.log(diceSet)

    for(let i = 0; i < diceSet.length; i++){
        if(diceSet[i].isHeld == false){
            diceSet[i].number = Math.floor((Math.random() * 6) + 1);
        }
        
        document.getElementById("die" + (i + 1) + "Img").src = "img/" + diceSet[i].number + ".png";
    } 
   
    console.log("After")
    console.log(diceSet)
    return theRoll;


}

这个意大利商标打扰了我对软件的构想。第二行(检查我的不可见网格)由一些项目组成:“空间” +“简介” +“空间” +画布。在标签和“画布”之间,我总是希望有5像素的空白,但是将意大利标签放在第4行和第1列中是不可能的。我该如何解决这个问题?

enter image description here

也许我可以使用两个不同的网格来解决我的问题,但是我认为这是不可能的..为了获得更多的澄清,您还可以阅读Bryan的评论。

2 个答案:

答案 0 :(得分:0)

网格不是对象,但是您可以根据需要深度嵌套框架,并且可以在每个框架中从头开始在每个框架中使用gridpackplace

这就是说,在您的特定情况下,如果您不希望“ test test test”标签将画布推到上方,则可以使它跨所有列。

test_label.grid(row=3, column=1, sticky="w", columnspan=3)

我不知道这是否是正确的解决方案,因为不清楚您要实现的目标。拥有1像素高的画布有点奇怪,最终的答案取决于您是否还有其他小部件以及它们的行为。


请记住,我只想使用网格几何管理器

我个人反对“我只想使用网格”的想法。 gridpackplace都有优点和缺点。您应该为作业使用正确的工具,而grid并不总是正确的工具。有时您可以使用pack编写一行代码,而在使用grid时可以完成三行代码。

答案 1 :(得分:0)

我重构了所有代码,作为如何删除小部件对主网格的依赖的示例。我使用pack的事实与此无关。将小部件组合放在自己的框架中时,依赖项已删除。

import tkinter as tk, tkinter.ttk as ttk

#common formatting for our custom Frames
class PackedFrame(tk.Frame):
    def __init__(self, master, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)
        #grid equivalent of filling the entire next available row
        self.pack(anchor='nw', side='top', fill='x')
        #inherit master background
        self.configure(bg=master['bg'])


#no dependency on master layout
class Introduction(PackedFrame):
    def __init__(self, master, **kwargs):
        PackedFrame.__init__(self, master, **kwargs)
        
        ttk.Label(self, text="Introduction", background=self['bg']).pack(side='left')
        
        #here we use a Separator instead of a Canvas
        ttk.Style().configure('TSeparator', background="#a0a0a0")
        ttk.Separator(self, orient='vertical').pack(side='right', fill='x', expand=True, padx=8)


#no dependency on master layout
class LangFormField(PackedFrame):
    def __init__(self, master, command, lang='english', **kwargs):
        PackedFrame.__init__(self, master, **kwargs)
        
        self.label = ttk.Label(self, background=self['bg'])
        self.label.pack(side='left', anchor='w')
        
        ttk.Entry(self).pack(side='left', anchor='w', fill='x', expand=True, padx=8)
        ttk.Button(self, text="...", width=5).pack(side='left', anchor='e')
        
        #call the appropriate language method
        if hasattr(self, lang) and callable(getattr(self, lang)):
            getattr(self, lang)()
        
    #TRANSLATIONS
    def italiano(self):
        self.label.configure(text='Ciao:')

    def english(self):
        self.label.configure(text='Hi:')


class Root(tk.Tk):
    def __init__(self, **kwargs):
        tk.Tk.__init__(self)
        self.configure(**kwargs)
        self.geometry("550x350")
        
        Introduction(self)
        
        ff = LangFormField(self, lambda:print('clicked'))

        ttk.Button(self, text="Italiano", command=ff.italiano).place(x=16, y=150)
        ttk.Button(self, text="English" , command=ff.english ).place(x=16, y=200)
        
        self.mainloop()


Root(background='#FFFFFF') if __name__ == "__main__" else None

    
    
相关问题