1

I tried using the Grid manager and wanted to place 4 buttons in 2 rows and 2 columns with this code:

button1 = Button(window, wrap="Button1", width=10)
button1.grid(row=0, column=0)
button1.pack()

button2 = Button(window, wrap="Button2", width=10)
button2.grid(row=0, column=1)
button2.pack()

button3 = Button(window, wrap="Button3", width=10)
button3.grid(row=1, column=0)
button3.pack()

button4 = Button(window, wrap="Button4", width=10)
button4.grid(row=1, column=1)
button4.pack()

And I got this error:

_tkinter.TclError: bad screen distance "Button1"

I don't know what a screen distance is and don't know how to fix a bad one. Can someone please help me there, please?

The Amateur Coder
  • 789
  • 3
  • 11
  • 33

2 Answers2

1

Solution


button1=tkinter.Button(window, text="button1", width=10) # create button 1
button1.grid(row=1,column=0) # arrange button 1 place


button2=tkinter.Button(window, text="button2", width=10) # create button 2
button2.grid(row=2,column=0) # arrange button 2 place

button3=tkinter.Button(window, text="button3", width=10) # create button 3
button3.grid(row=1,column=2) # arrange button 3 place

button4=tkinter.Button(window, text="button4", width=10) # create button 4
button4.grid(row=2,column=2) # arrange button 4 place

What to do to fix that?


You need to remove the command called button1.pack() and from other buttons too, so it should be as the code attached. Also change warp to text when creating button.

Omar The Dev
  • 123
  • 1
  • 14
  • Also, one optional thing which you can do, is set all buttons to ``width=10, height=10`` which makes them look much better when executing the code – Omar The Dev Feb 01 '22 at 04:45
0

Change wrap to text and remove the .pack() as you have already used .grid()

button1 = Button(window, text="Button1", width=10)
button1.grid(row=0, column=0)

button2 = Button(window, text="Button2", width=10)
button2.grid(row=0, column=1)

button3 = Button(window, text="Button3", width=10)
button3.grid(row=1, column=0)

button4 = Button(window, text="Button4", width=10)
button4.grid(row=1, column=1)
Sister Coder
  • 324
  • 1
  • 2
  • 12
  • I'm sorry,but that generated a different Error: `_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack` – Collin Lehmann Jan 30 '22 at 16:07
  • That error is usually caused by using `.pack()` and `.grid()` on the same object. If this isn't all of your code look through your code and see where you've used both for the same object and remove the `.pack()` – Sister Coder Jan 30 '22 at 16:32
  • do you mean all objects in my script? or only the 4 buttons? I inly used .grid on the 4 buttons – Collin Lehmann Jan 30 '22 at 16:36
  • I'm sorry,but that didnt work... – Collin Lehmann Jan 30 '22 at 16:43
  • Did you put anything else in the `window` with `.pack()` ? Or just the buttons? – Sister Coder Jan 30 '22 at 16:44
  • I replaced every .pack() with a .grid() And got this Error '''_tkinter.TclError: bad option "-fill": must be -column, -columnspan, -in, -ipadx, -ipady, -padx, -pady, -row, -rowspan, or -sticky''' – Collin Lehmann Jan 30 '22 at 16:46
  • It might be easier to create a `LabelFrame`, put the buttons in the LabelFrame (with the grid format then pack the frame to `window` along with the other objects. Have you tried the code in the answer separate from the rest of your code as it worked in my end – Sister Coder Jan 30 '22 at 16:53