Home > other >  Tkinter grid manager bad screen distance
Tkinter grid manager bad screen distance

Time:02-01

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?

CodePudding user response:

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.

CodePudding user response:

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)
  •  Tags:  
  • Related