简体   繁体   English

带有列表框的Tkinter / Python网格布局

[英]Tkinter / Python grid layout with listboxes

I'm having some trouble getting everything to line up properly using a grid with Tkinter and Python. 我在使用Tkinter和Python的网格正确排列所有内容时遇到了一些麻烦。 I want to have the menu bar at the very top, and then two listboxes (one on each side), with a couple buttons between them in the middle of the window. 我希望菜单栏位于最顶部,然后是两个列表框(每侧一个),窗口中间有几个按钮。

Currently, both listboxes are appearing on the bottom right side of the window, and the buttons are in the upper left corner, overlapping the menu buttons. 目前,两个列表框都出现在窗口的右下方,按钮位于左上角,与菜单按钮重叠。

How do I deal with the grid layout of a listbox considering their size. 考虑到它们的大小,我如何处理列表框的网格布局。 Can they occupy more than one grid cell? 他们可以占用多个网格单元吗? For example, if I place a listbox in (row=1, column = 1) and another in row (row = 1, column = 2), would Tkinter automatically widen the cell so that it's the width of the listbox, or would it just overlap the listboxes? 例如,如果我将一个列表框放在(row = 1,column = 1)而另一个放在行中(row = 1,column = 2),Tkinter是否会自动加宽单元格以使其成为列表框的宽度,或者是否只是重叠列表框?

def __init__(self,root):
    self.frame = Frame(root, width=500)
    self.frame.pack()
    self.BuildMainWindow(self.frame)
    self.ListboxSet = 0 
    self.frame.grid()
    return

#displays the menu bar and options
def BuildMainWindow(self, frame):
    menubar = Frame(frame,relief=RAISED,borderwidth=1)
    menubar.pack()

    mb_file = Menubutton(menubar,text='file')
    mb_file.menu = Menu(mb_file)
    mb_file.menu.add_command(label='open', command = self.openfile)
    mb_file.grid(row = 1, column = 1)

    mb_edit = Menubutton(menubar,text='edit')
    mb_edit.menu = Menu(mb_edit)
    mb_edit.grid(row=1, column = 3)

    mb_file['menu'] = mb_file.menu
    mb_edit['menu'] = mb_edit.menu

#upon selecting a directory from the menu and listboxes/buttons are created
def BuildListbox(self, directory):
    self.listbox1 = Tkinter.Listbox()
    self.listbox2 = Tkinter.Listbox()
    self.listbox1.grid(row=2, column=1)
    self.listbox2.grid(row=2 ,column=10)
    self.listbox2.bind('<<ListboxSelect>>', lambda e:SortActions.GetWindowIndex(self,e))
    self.listbox2.bind('<B1-Motion>', lambda e:SortActions.MoveWindowItem(self,e))

    i = 0
    for filename in sorted(os.listdir(directory)):
        self.listbox1.insert(i, filename)
        i = i + 1

    self.bAddToListTwo = Button(self.frame, text = "->", command = lambda:SortActions.AddToListTwo(self,self.listbox1.curselection()))
    self.bAddToListTwo.grid(row=5, column = 5)
    self.bAddAll = Button(self.frame, text = "Add All To Playlist", command = lambda:SortActions.AddAllFiles(self))
    self.bAddAll.grid(row=6, column = 5)
    self.bRemoveFromListTwo = Button(self.frame, text = "Remove From Playlist", command = lambda:SortActions.RemoveFromListTwo(self,self.listbox2.curselection()))
    self.bRemoveFromListTwo.grid(row=7, column = 5)
    self.bSavePlaylist = Button(self.frame, text = "Save Playlist", command = lambda:SortActions.SaveList(self))
    self.bSavePlaylist.grid(row=8, column = 5)

update - I got it to work. 更新 - 我得到了它的工作。 As I suspected, I had some weird frame configuration during the initialization. 我怀疑,在初始化期间我有一些奇怪的帧配置。

def __init__(self,root):
    self.BuildMainWindow(root)
    self.ListboxSet = 0 
    return

#displays the menu bar and options
def BuildMainWindow(self, root):
    menubar = Menu(root)
    root.config(menu=menubar)

    # Create a menu button labeled "File" that brings up a menu
    filemenu = Menu(menubar)
    menubar.add_cascade(label='File', menu=filemenu)

    # Create entries in the "File" menu
    # simulated command functions that we want to invoke from our menus
    filemenu.add_command(label='Open', command=self.openfile)
    filemenu.add_separator(  )
    filemenu.add_command(label='Quit', command=sys.exit)

#upon selecting a directory from the menu and listboxes/buttons are created
def BuildListbox(self, directory):
    self.listbox1 = Tkinter.Listbox()
    self.listbox2 = Tkinter.Listbox()
    self.listbox1.grid(row=1, column=1, rowspan = 4, columnspan = 5)
    self.listbox2.grid(row=1 ,column=15, rowspan = 4, columnspan = 5)
    self.listbox2.bind('<<ListboxSelect>>', lambda e:SortActions.GetWindowIndex(self,e))
    self.listbox2.bind('<B1-Motion>', lambda e:SortActions.MoveWindowItem(self,e))

    i = 0
    for filename in sorted(os.listdir(directory)):
        self.listbox1.insert(i, filename)
        i = i + 1

    self.bAddToListTwo = Button(text = "->", command = lambda:SortActions.AddToListTwo(self,self.listbox1.curselection()))
    self.bAddToListTwo.grid(row=1, column = 6)
    self.bAddAll = Button(text = "Add All To Playlist", command = lambda:SortActions.AddAllFiles(self))
    self.bAddAll.grid(row=2, column = 6)
    self.bRemoveFromListTwo = Button(text = "Remove From Playlist", command = lambda:SortActions.RemoveFromListTwo(self,self.listbox2.curselection()))
    self.bRemoveFromListTwo.grid(row=3, column = 6)
    self.bSavePlaylist = Button(text = "Save Playlist", command = lambda:SortActions.SaveList(self))
    self.bSavePlaylist.grid(row=4, column = 6)

Tkinter widgets can occupy more than 1 grid-cell, but you need to manage the layout yourself using the columnspan and rowspan keywords to .grid . Tkinter小部件可以占用多个网格单元,但您需要使用columnspanrowspan关键字.grid管理布局.grid

For example, to get the layout: 例如,要获得布局:

 --------------------------------------------
 |      Button1      |  Button2 |           |
 --------------------------------  Button3  |
 | Label1  |  Label2 |  Label3  |           |
 --------------------------------------------

You'd need to do something like: 你需要做类似的事情:

Button1.grid(row=0,column=0,columnspan=2)
Button2.grid(row=0,column=2)
Button3.grid(row=0,column=4,rowspan=2)
Label1.grid(row=1,column=0)
Label2.grid(row=1,column=1)
Label3.grid(row=1,column=2)

very minimal script 非常小的脚本

import Tkinter as tk

root = tk.Tk()
Button1 = tk.Button(root,text="Button1")
Button2 = tk.Button(root,text="Button2")
Button3 = tk.Button(root,text="Button3")
Label1 = tk.Label(root,text="Label1")
Label2 = tk.Label(root,text="Label2")
Label3 = tk.Label(root,text="Label3")

Button1.grid(row=0,column=0,columnspan=2)
Button2.grid(row=0,column=2)
Button3.grid(row=0,column=4,rowspan=2)
Label1.grid(row=1,column=0)
Label2.grid(row=1,column=1)
Label3.grid(row=1,column=2)

root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM