1

使用 Tkinter 我创建了 5 个框架,每个包含

  • frame0 -labelEntry box一个button(Enter)
  • frame1 -labellist box左侧
  • frame2 -2 buttons(select & unselect)在中心
  • frame3 -labellistbox右侧
  • 框架4 -2 buttons(done & clear)

     ----------------------------------------
     |                 frame0               |
     ----------------------------------------
     |            |            |            |
     |  frame1    |   frame2   |  frame3    |
     |            |            |            |
     ----------------------------------------
     |                 frame4               |
     ----------------------------------------
    

这是我的代码:

from Tkinter import *

def toplevelwin():

    def clear():
        return

    def select():
        return

    def unselect():
        return

    def done():
        return

    def enter():
        return

    window = Toplevel()

    frame0 = Frame(window)
    frame0.grid(row=0, column=0, sticky=W, padx=5, pady=5, columnspan=2)
    frame0.grid_columnconfigure(1,weight=2)
    lblentry = Label(frame0, text="Entery Box:")
    lblentry.grid(row=0, column=0, sticky=W)
    entrybx = Entry(frame0)
    entrybx.grid(row=1,column=0,sticky=N+S+E+W, columnspan=2)
    entrybt = Button(frame0, text=' Enter ', command=enter)
    entrybt.grid(row=1,column=2,sticky=N+W, padx=3)

    frame1 = Frame(window)
    frame1.grid(row=1, column=0, sticky=E+W, padx=5, pady=5)
    lblshow_lst = Label(frame1, text="List Box 1:")
    lblshow_lst.grid(row=0,sticky=W)
    show_lst = Listbox(frame1)
    show_lst.grid(row=1,sticky=W)

    frame2 = Frame(window)
    frame2.grid(row=1, column=1, sticky=W)
    frame2.grid_columnconfigure(1,weight=1)
    selbtn = Button(frame2, text='Select', command=select)
    selbtn.grid(row=0, padx=5, sticky=E+W)
    selbtn.grid_columnconfigure(1,weight=1)
    uselbtn = Button(frame2, text='Unselect', command=unselect)
    uselbtn.grid(row=1, padx=5, sticky=E+W)
    uselbtn.grid_columnconfigure(1,weight=1)

    frame3 = Frame(window)
    frame3.grid(row=1, column=2, sticky=W, padx=5, pady=5)
    lblsel_lst = Label(frame3, text="List Box 2:")
    lblsel_lst.grid(row=0,sticky=W)
    sel_lst = Listbox(frame3)
    sel_lst.grid(row=1, column=0, sticky=W)

    frame4 = Frame(window)
    frame4.grid(row=2, column=0, sticky=E, padx=5, pady=5)
    Button(frame4, text=' Done ', command=done).grid(row=0, column=0, padx=7 ,pady=2)
    Button(frame4, text='Clear', command=clear).grid(row=0,column=1, padx=7,pady=2)
    window.wait_window(window)


root = Tk()
toplevelwin()
root.mainloop()

这就是我的窗口现在的样子:

在此处输入图像描述

我的问题是:

  • 我如何扩展frame0uptill frame3。即,我希望将entry box延伸到 frame3 的末端,button(enter)使其右侧保持原样。
  • 我希望将bottom frame's按钮(完成并清除)放在窗口的右侧

尝试:

  • 我试图增加入口小部件的大小,这反过来又将 frame2 和 frame3 推到了右侧。

  • used sticky=N+S+W+E,将它推到窗口的中心

  • 也尝试过sticky=W包含 2 个按钮的底部框架。他们移动到中心但不正确

4

2 回答 2

3

使用网格时,要使小部件能够扩展或缩小,您需要设置其sticky参数并设置其网格单元格的weight. 网格的每一列和每一行都有一个权重

默认情况下,所有列和行的权重都是 0,这意味着它们将缩小到最小的有效大小。

要使其在东西方向上扩展,请使用 增加其列权重 grid_columnconfigure

要使其向南北方向扩展,请使用 增加其行权重 grid_rowconfigure


所以,对于frame0

  • 用于columnspan=3制作frame0跨度 3 列。
  • 用于sticky='WE'使其frame0牢牢固定在其单元的西侧和东侧。
  • 用于frame0.grid_columnconfigure(0, weight=1)将网格单元的权重增加到 1。

对于frame4,

frame4 = Frame(window)
frame4.grid(row=2, column=0, sticky='E', padx=5, pady=5, columnspan=3)

frame4如果它的单元格将其粘在东侧,将起作用。请注意,没有重量,frame4也会缩小;它只是在紧贴东侧时收缩,因此它包含的按钮最终位于右侧。

如果您想frame4如图所示跨越从西到东的整个空间,请使用

frame4.grid(row=2, column=0, sticky='WE', padx=5, pady=5, columnspan=3)
frame4.grid_columnconfigure(0, weight=1)
Button(frame4, text=' Done ', command=done).grid(
    row=0, column=0, padx=7, pady=2, sticky='E')
Button(frame4, text='Clear', command=clear).grid(
    row=0, column=1, padx=7, pady=2, sticky='E')

在此处输入图像描述

from Tkinter import *

def toplevelwin():
    def clear(): return
    select = unselect = done = enter = clear
    window = Toplevel()

    frame0 = Frame(window)
    frame0.grid(row=0, column=0, sticky='WE', padx=5, pady=5, columnspan=3)
    frame0.grid_columnconfigure(0, weight=1)
    lblentry = Label(frame0, text="Entry Box:")
    lblentry.grid(row=0, column=0, sticky='W')
    entrybx = Entry(frame0)
    entrybx.grid(row=1, column=0, sticky='NSEW', columnspan=2)
    entrybt = Button(frame0, text=' Enter ', command=enter)
    entrybt.grid(row=1, column=2, sticky='NW', padx=3)

    frame1 = Frame(window)
    frame1.grid(row=1, column=0, sticky='EW', padx=5, pady=5)
    lblshow_lst = Label(frame1, text="List Box 1:")
    lblshow_lst.grid(row=0, sticky='W')
    show_lst = Listbox(frame1)
    show_lst.grid(row=1, sticky='W')

    frame2 = Frame(window)
    frame2.grid(row=1, column=1, sticky='W')
    selbtn = Button(frame2, text='Select', command=select)
    selbtn.grid(row=0, padx=5, sticky='EW')
    uselbtn = Button(frame2, text='Unselect', command=unselect)
    uselbtn.grid(row=1, padx=5, sticky='EW')

    frame3 = Frame(window)
    frame3.grid(row=1, column=2, sticky='W', padx=5, pady=5)
    lblsel_lst = Label(frame3, text="List Box 2:")
    lblsel_lst.grid(row=0, sticky='W')
    sel_lst = Listbox(frame3)
    sel_lst.grid(row=1, column=0, sticky='W')

    frame4 = Frame(window)
    frame4.grid(row=2, column=0, sticky='WE', padx=5, pady=5, columnspan=3)
    frame4.grid_columnconfigure(0, weight=1)
    Button(frame4, text=' Done ', command=done).grid(
        row=0, column=0, padx=7, pady=2, sticky='E')
    Button(frame4, text='Clear', command=clear).grid(
        row=0, column=1, padx=7, pady=2, sticky='E')

    window.wait_window(window)


root = Tk()
toplevelwin()
root.mainloop()
于 2015-07-24T12:23:04.430 回答
1

您不需要框架(底部的按钮除外)。Juste让你的小部件像这样:

entrybx = Entry(window)
entrybx.grid(row=0,column=0,sticky=N+S+E+W, columnspan=2) # columnspan= 3 better?

sticky=W当您希望列左侧的小部件sticky=W +E居中和sticky=E右侧时使用。

对于底部的按钮,不要忘记columnspan

frame4 = Frame(window)
frame4.grid(row=2, column=0, sticky=E, padx=5, pady=5, columnspan=3)
Button(frame4, text=' Done ', command=done).grid(row=0, column=0, padx=7 ,pady=2, sticky=E)
Button(frame4, text='Clear', command=clear).grid(row=0,column=1, padx=7,pady=2, sticky=E)
于 2015-07-24T10:11:14.680 回答