2014-11-03 112 views
-1

我正在创建一个GUI,其中我有一个TextBox和几个Button s。使用Tkinter的Python GUI - 小部件定位不正确

问题是,布局似乎是混乱的。当我增加行数以便我能够很好地分隔空间时,它没有任何区别。我不确定我犯了什么错误。

代码:

from Tkinter import * 
from tkFileDialog import * 

gui = Tk() #create an object 
gui.title("xyz") 
gui.geometry("900x300") 

GuiLabel1 = Label(gui,text="hi everyone!!!!!!") 
GuiLabel1.grid(row=0, column=0) 
GuiLabel2 = Label(gui,text="File") 
GuiLabel2.grid(row=1, column=0) 


bar=Entry(gui) 
bar.grid(row=1, column=1) 



button1= Button(gui, text="Browse") 
button1.grid(row=1, column=2) 
button2= Button(gui, text="Process") 
button2.grid(row=2, column=2) 

button3= Button(gui, text="ABC") 
button3.grid(row=3, column=0) 

button4= Button(gui, text="ABC") 
button4.grid(row=3, column=1) 

button5= Button(gui, text="ABC") 
button5.grid(row=3, column=2) 

button6= Button(gui, text="ABC") 
button6.grid(row=3, column=3) 

button7= Button(gui, text="ABC") 
button7.grid(row=3, column=4) 


gui.mainloop() 

请参见下面的图像的图形用户界面的截图:

screenshot of the GUI

+2

究竟做*“炒” *是什么意思?该输出看起来很像我期望的。空行默认压缩到零高度;如果你想填充,添加填充。 – jonrsharpe 2014-11-03 16:07:09

+0

从某种意义上说,这些小工具并没有按照有序的方式定位。就像第二个ABC并不完全在第一个旁边。我需要在流程按钮下方的空行。 ABC的间距不相等!!!!!!!!!! – blackfury 2014-11-03 16:35:29

+0

然后,您需要向Tkinter提供更多关于如何布置小部件的信息(行和列大小,填充,单元格中每个小部件应该定位的位置等)。默认情况下,每行和每列都被自动调整以适合最大的窗口部件,因此第一列的宽度与'Label'的宽度相同,第二列的宽度与最大的'Button'宽度相同(第二宽度为“TextBox”一个具有最广泛的“文本”),等等......这就是“网格”所做的 - 将小部件放入网格中。 *(另外,请注意,一个感叹号通常(超过)足够。)* – jonrsharpe 2014-11-03 16:37:25

回答

0

您可以使用“WIDTH =”参数来均匀地间隔开的小部件看到http://effbot.org/tkinterbook/button.htm和行/ columnconfigure显示空行/列

from Tkinter import * 

gui = Tk() #create an object 
gui.title("xyz") 
gui.geometry("900x300") 

GuiLabel1 = Label(gui,text="hi everyone!!!!!!") 
GuiLabel1.grid(row=0, column=0) 

GuiLabel2 = Label(gui,text="File") 
GuiLabel2.grid(row=3, column=0) 

bar=Entry(gui) 
bar.grid(row=1, column=1) 

button1= Button(gui, text="Browse", width=test_width) 
button1.grid(row=1, column=2) 
button2= Button(gui, text="Process", width=test_width) 
button2.grid(row=2, column=2) 

test_width=20 
for ctr in range(4): 
    button= Button(gui, text="ABC"+str(ctr+1), width=test_width) 
    button.grid(row=3, column=ctr) 

## empty column between 
gui.columnconfigure(4, minsize=50) 
button7= Button(gui, text="ABC") 
button7.grid(row=3, column=5) 

gui.mainloop() 
-3

我想你可能会寻找的

pack() 
grid() 

混合还有元素Frame,它允许你'组'elem其中的一些元素被视为一个元素。

我无法修复你的代码,但我建议你看看这些,并尝试一下。

+1

混合'grid'和'pack'是一个可怕的想法,并且经常导致无限循环(请参阅http://stackoverflow.com /一个/3001761分之3968033)。 – jonrsharpe 2014-11-03 17:24:48

+0

如果你不是在同一个主人做的话,你可以把它混合起来。感谢您的链接,我曾经想过。 – User 2014-11-03 17:34:30

+0

考虑到OP *不是*使用多个主人,这可能值得非常清楚,你不觉得吗? – jonrsharpe 2014-11-03 17:35:21

1

这些小工具看起来非常像您告诉他们出现的。问题是,你依靠很多的定位默认值,这可能是为什么他们没有按照你认为他们应该的方式出现在屏幕上。

当您使用网格时,您应该几乎总是包含sticky选项来定义窗口小部件将如何填充它所放置的单元格。您经常需要使用填充来增加该窗格。最后,你通常应该给予至少一行和一列非零权重。

另外,请记住您正在创建网格。如果网格中有一个非常宽的项目,那将导致整个列变宽。如果将较小的项目放在后续行中,它们将默认以该列为中心。