使用网格在tkinter gui中设置不均匀的单元格高度

时间:2012-05-31 02:31:46

标签: python user-interface tkinter ttk

这是我的代码:

from tkinter import *
from tkinter.ttk import *

class App:

    def __init__(self, master):
        #Frame.__init__(self, master)

        fuzz = Entry(master, width = 20).grid(column=2,row=2)
        Label (text='Fuzz:').grid(column=1,row=2)

        smoothing = Entry(master, width = 20).grid(column=2,row=3,sticky=N)
        Label (text='Smoothing:').grid(column=1,row=3,sticky=N)

        rendering = Entry(master, width = 20).grid(column=2,row=4,sticky=N)
        Label (text='rendering:').grid(column=1,row=4,sticky=N)


        colors = Listbox(master, selectmode="extended", activestyle="dotbox")

        for item in ["color_1", "color_2", "color_3", "color_4"]:
            colors.insert(END, item)
        colors.grid(row=3,column=3, columnspan =1, padx=10, rowspan=2)

        pics = Listbox(master, selectmode="extended", activestyle="dotbox")

        for item in ["pic_1", "pic_2", "pic_3", "pic_4"]:
            pics.insert(END, item)
        pics.grid(row=3,column=4, columnspan=1, padx=10, rowspan=2)

        addcolor = Button(master, text="add color", )
        addcolor.grid(column=3,row=1, sticky=W+E, padx=10)
        remcolor = Button(master, text="remove color", )
        remcolor.grid(column=3,row=2, sticky=W+E, padx=10)

        addpic = Button(master, text="add pic", )
        addpic.grid(column=4,row=1, sticky=W+E, padx=10)
        rempic = Button(master, text="remove pic",)
        rempic.grid(column=4,row=2, sticky=W+E, padx=10)



root = Tk()

app = App(root)

root.mainloop()

这就是我想要做的事:http://i.imgur.com/sizHT.png

基本上,列表框跨越两行,列表框左侧的两个单元格是列表框高度的一半。但是有可能让单元格与默认高度一样短吗?

I have tried:
    changing the rowspan to 1 
    Having the listbox occupy 10 rows (results in odd padding around the cells)

1 个答案:

答案 0 :(得分:1)

您必须强制使用平均增长每行的默认机制。您必须使用grid_rowconfigure来设置这些行的“权重”。权重是在行之间分配额外空间的相对指数。

在你的情况下:

    master.grid_rowconfigure(3, weight=0)
    master.grid_rowconfigure(4, weight=1)

因此,第3行(带平滑)告诉不要占用额外的空间。