0

I am creating a GUI wherein I have a TextBox, and a few Buttons.

The problem is, the layout seems to be scrambled. And when I increase the row number so that I could space out well, it doesn't make any difference. I am not sure where I have made a mistake.

Code:

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()

See the below image for the screenshot of the GUI:

screenshot of the GUI

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
blackfury
  • 675
  • 3
  • 11
  • 22
  • 2
    What exactly does *"scrambled"* mean? That output looks pretty much how I'd expect. Empty rows are compressed to zero height by default; if you want padding, add padding. – jonrsharpe Nov 03 '14 at 16:07
  • Scrambled in the sense, the widgets are not positioned in an orderly manner. Like the second ABC is not exactly right next to the first one. And i need an empty row below process button. The spacing bw the ABCs are not equal!!!!!!!!!! – blackfury Nov 03 '14 at 16:35
  • Then you need to give Tkinter more information on how you want the widgets laid out (row and column sizes, padding, where in the cell each widget should be oriented, etc.). By default, each row and column is autosized to fit the largest widget, hence the first column is as wide as the `Label`, the second as the `TextBox`, the third as wide as the widest `Button` (which is the one with the widest `text`), and so on... That's what `grid` does - puts the widgets into a grid. *(Also, please note that one exclamation mark is usually (more than) enough.)* – jonrsharpe Nov 03 '14 at 16:37

3 Answers3

1

The widgets are appearing pretty much exactly as you have told them to appear. The problem is, you are relying on a lot of defaults for positioning which is probably why they don't appear on the screen the way you think they should.

When you use grid, you should almost always include the sticky option to define how a widget will fill the cell it has been placed in. You often need to augment that with padding. Finally, you should generally give at least one row and one column a non-zero weight.

Also, remember that you're creating a grid. If you have a very wide item in the grid, that will cause the entire column to be wide. If you place smaller items in subsequent rows they will by default be centered in that column.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

You can use the "width=" parameter to evenly space widgets see http://effbot.org/tkinterbook/button.htm and row/columnconfigure to show empty rows/columns

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

I Think you may be looking for a mixture of

pack()
grid()

and also the element Frame which allows you to 'group' elements in it that count as one element.

I can not fix your code but I suggest you look into those and have a try.

User
  • 14,131
  • 2
  • 40
  • 59
  • 1
    Mixing `grid` and `pack` is a terrible idea, and often leads to infinite loops (see e.g. http://stackoverflow.com/a/3968033/3001761). – jonrsharpe Nov 03 '14 at 17:24
  • You can mix it if you do not do it in the same master. Thanks for the link, I once wondered. – User Nov 03 '14 at 17:34
  • Given that the OP *isn't* using multiple masters, that's probably worth making pretty clear, don't you think? – jonrsharpe Nov 03 '14 at 17:35