如何使用grid_rowconfigure和grid_columnconfigure创建一个多列表框

时间:2019-10-23 18:12:05

标签: python tkinter

我想使用grid_columnconfigure和grid_rowconfigure创建一个Multilistbox。我有以下代码,当我最大化窗口时,它不起作用,因为我不知道如何在Multilistbox中设置columnconfigure和rowconfigure。

最大化窗口时,我的代码如下所示: Working Code

当我最大化窗口时,我的期望是这样的: expected result when I will maximize the window 我的代码在这里:

from Tkinter import *

class MultiListbox(Frame):
    def __init__(self, master, lists):
        Frame.__init__(self, master)
        self.lists = []
        Label(self,text="Product Name",borderwidth=1,
              relief=RAISED).grid(row=0,column=0,columnspan=6,sticky=N + S + E + W)
        Label(self,text="Product Name",borderwidth=1,
              relief=RAISED).grid(row=1,column=0,columnspan=1,sticky=N + S + E + W)
        Label(self,text="Product Name",borderwidth=1,
              relief=RAISED).grid(row=1,column=1,columnspan=3,sticky=N + S + E + W)
        Label(self,text="Product Name",borderwidth=1,
              relief=RAISED).grid(row=1,column=4,columnspan=2,sticky=N + S + E + W)
        for num, (l, w) in enumerate(lists):
            frame = Frame(self)
            frame.grid(row=2,column=num)
            Label(frame, text=l, borderwidth=1, relief=RAISED).grid(row=0,column=0,sticky=N + S + E + W)
            lb = Listbox(frame, width=w, borderwidth=0, selectborderwidth=0,
                         relief=FLAT, exportselection=FALSE)
            lb.grid(row=1,column=0)
            self.lists.append(lb)
            lb.bind('<B1-Motion>', lambda e, s=self: s._select(e.y))
            lb.bind('<Button-1>', lambda e, s=self: s._select(e.y))
            lb.bind('<Leave>', lambda e: 'break')
            lb.bind('<B2-Motion>', lambda e, s=self: s._b2motion(e.x, e.y))
            lb.bind('<Button-2>', lambda e, s=self: s._button2(e.x, e.y))
        Label(self, borderwidth=1, relief=RAISED, text=" ").grid(row=0,column=7,sticky=N + S + E + W,rowspan=3)
        sb = Scrollbar(self, orient=VERTICAL)
        sb.grid(row=2,column=7,sticky="ns")
        self.lists[0]['yscrollcommand'] = sb.set

if __name__ == '__main__':
    tk = Tk()
    Label(tk, text='MultiListbox').pack()
    mlb = MultiListbox(tk, (('Quantity', 20), ('Price', 20), ('Quantity', 20), ('Price', 20),('Quantity', 20), ('Price', 20),))
    mlb.pack(expand=YES, fill=BOTH)
    tk.mainloop()

0 个答案:

没有答案