Tkinter 网格管理器屏幕距离错误

Tkinter grid manager bad screen distance

我尝试使用网格管理器并希望使用以下代码在 2 行 2 列中放置 4 个按钮:

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

我得到了这个错误:

_tkinter.TclError: bad screen distance "Button1"

我不知道什么是屏幕距离,也不知道如何解决错误的问题。有人可以帮我吗?

wrap 更改为 text 并删除 .pack(),因为您已经使用 .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)

解决方案


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

如何解决这个问题?


您需要从其他按钮中删除名为 button1.pack() 的命令,因此它应该与附加的代码一样。在创建按钮时也将 warp 更改为 text