tkinter gui微调小部件布局

时间:2015-12-16 18:06:59

标签: python user-interface tkinter widget

这是一个回答帖子的后续内容 - 我认为即使它与之相关,也可以添加到已回答的帖子中。“

总的来说,我对布局非常满意(布莱恩·O here完成了繁重的工作。)

现在我正在尝试微调一些小部件,我似乎无法轻推一下。为了移动小部件,我似乎需要对其他帧进行分层。这似乎是用霰弹枪杀死苍蝇,但我知道什么?

我想轻推“添加边缘”按钮,因此它与左边的数字输入窗口小部件之间有一些空间。

我也希望在底部的ok和cancel按钮之间留出一些空间。尝试通过padx添加填充,并且实现分层帧会使事情bad layout truncated central region非常糟糕。我猜几何传播意味着使用padx不是正确的方法。

我似乎无法轻推我想要的小部件。我的问题具体:使用我的代码库,你如何建议我做这些微调?

THX

current gui layout

代码:

 from Tkinter import *

root2 = Tk()
root2.title('Model Definition')
root2.geometry('{}x{}'.format(460, 350))

# functions/commands

def get_list(event):
    global seltext

    """
    read the listbox selection and put the result somewhere
    """

    # get selected line index
    index = data_list.curselection()[0]

    # get the line's text
    seltext = data_list.get(index)

    root2.update_idletasks()

# create all of the main containers
top_frame = Frame(root2, bg='cyan', width = 450, height=50, pady=6)
center = Frame(root2, bg='gray2', width=50, height=40, padx=3, pady=3)
btm_frame = Frame(root2, bg='plum4', width = 450, height = 45, pady=3)

btm_frame2_outer = Frame(root2, bg='lavender', width = 450, height = 60, pady=3)
btm_frame2 = Frame(btm_frame2_outer, bg='green', width = 350, height = 60, pady=3)
btm_frame2_cntr = Frame(btm_frame2_outer, bg='gray', width = 50, padx=7)

# layout all of the main containers
root2.grid_rowconfigure(1, weight=1)
root2.grid_columnconfigure(0, weight=1)

top_frame.grid(row=0, sticky="ew")
center.grid(row=1, sticky="nsew")
btm_frame.grid(row = 3, sticky="ew")

btm_frame2_outer.grid(row = 4, sticky="ew")
btm_frame2.grid(row = 1, columnspan = 2, sticky="ew")
btm_frame2_cntr.grid(row = 1, column = 4, sticky='ew')

# create the widgets for the top frame
model_label = Label(top_frame, text = 'Model Dimensions')
width_label = Label(top_frame, text = 'Width:')
length_label = Label(top_frame, text = 'Length:')
entry_W = Entry(top_frame, background="pink")
entry_L = Entry(top_frame, background="orange")

# layout the widgets in the top frame
model_label.grid(row = 0, column = 0, pady=5)
width_label.grid(row = 1, column = 0, sticky = 'e')
length_label.grid(row = 1, column = 2)
entry_W.grid(row = 1, column = 1)
entry_L.grid(row = 1, column = 3)

# create the center widgets
center.grid_rowconfigure(0, weight=1)
center.grid_columnconfigure(1, weight=1)

ctr_left = Frame(center, bg='blue', width=100, height=190)
ctr_mid = Frame(center, bg='yellow', width=250, height=190, padx=3, pady=3)
ctr_right = Frame(center, width=100, height=190, padx=3, pady=3)

ctr_left.grid(row=0, column = 0, sticky="ns")
ctr_mid.grid(row=0, column = 1, sticky="nsew")
ctr_right.grid(row=0, column = 2, sticky="ns")

# decorate the center frame widgets
# left
shift_up_label = Label(ctr_left, text = 'Shift Up')
shift_down_label = Label(ctr_left, text = 'Shift Down')
cut_label = Label(ctr_left, text = 'Cut')
copy_label = Label(ctr_left, text = 'Copy')
paste_label = Label(ctr_left, text = 'Paste')
# center
data_list = Listbox(ctr_mid, bg='snow2', width='55')
yscroll = Scrollbar(ctr_mid, command=data_list.yview, orient=VERTICAL)
# right
status_label = Label(ctr_right, text = 'Status', bg = 'green', height = 11)

#####################################################################
# layout the center widgets
#####################################################################
#left
shift_up_label.grid(row = 0, column = 0, pady = '7', sticky = 'nsew')
shift_down_label.grid(row = 1, column = 0, pady = '7', sticky = 'nsew')
cut_label.grid(row = 2, column = 0, pady = '7', sticky = 'nsew')
copy_label.grid(row = 3, column = 0, pady = '7', sticky = 'nsew')
paste_label.grid(row = 4, column = 0, pady = '7', sticky = 'nsew')

# center
data_list.grid(row=0, column=0, sticky='ns')
yscroll.grid(row=0, column=0, sticky='ens')

# right
status_label.grid(row = 2, column = 0, rowspan = 4, sticky = 'nsew')
# create the bottom widgets
# layout the bottom widgets

#####################################################################
# create bottom widgets
#####################################################################
label_label = Label(btm_frame, text = 'Label:', padx = '4')
entry_label = Entry(btm_frame, background="orange")
entry_number = Entry(btm_frame, background="cyan")
number_label = Label(btm_frame, text = 'Number:', padx = '4')
add_btn = Button(btm_frame, text='Add Edges', padx = '12')
ok_btn = Button(btm_frame2_cntr, text='OK', padx = '5')
cancel_btn = Button(btm_frame2_cntr, text='Cancel', padx = '12')

#####################################################################
# layout the bottom widgets
#####################################################################
label_label.grid(row = 1, column = 1, sticky = 'ew')
entry_label.grid(row = 1, column = 2, sticky = 'w')
number_label.grid(row = 1, column = 3, sticky = 'w')
entry_number.grid(row = 1, column = 4, sticky = 'e')
add_btn.grid(row = 1, column = 6, sticky = 'e')
ok_btn.grid(row = 0, column = 3, sticky = 'ew')
cancel_btn.grid(row = 0, column = 4, sticky = 'e')

# commands/bindings
data_list.configure(yscrollcommand=yscroll.set)
data_list.bind('<ButtonRelease-1>', get_list)

root2.mainloop()

1 个答案:

答案 0 :(得分:1)

  

我想轻推“添加边缘”按钮,因此它有一些空间   介于它和左边的数字输入小部件之间。

当您在窗口小部件上调用grid时,有两个选项可控制窗口小部件两侧的间距:padxpady。您可以指定适用于双方的单个值(例如:padx=20将在小部件的左侧和右侧添加20个像素),或者您可以提供两个元组(例如:{{1}只会在右边添加20个像素。

例如,要“轻推”“添加边缘”按钮,只需添加一些填充:

padx=(0,20)
  

我也希望在确定和取消按钮之间留出一些空间   在底部。

同样,add_btn.grid(row = 1, column = 6, sticky = 'e', padx=(20, 0)) 是解决方案:

padx