Python按钮位置

时间:2016-02-15 17:13:44

标签: python button tkinter

我目前正在尝试编写一个井字游戏。 首先,我创建了9个应该填满窗口的按钮。 但我无法将它们置于这样的顺序中。这是我的代码:

from tkinter import *

root=Tk()
root.geometry('300x300')

tl=Button(root)
tm=Button(root)
tr=Button(root)
ml=Button(root)
mm=Button(root)
mr=Button(root)
ll=Button(root)
lm=Button(root)
lr=Button(root)

photo1=PhotoImage(file="/Users/apple/Desktop/images Tkinter/arrowhead.gif")
photo2=PhotoImage(file="/Users/apple/Desktop/images     Tkinter/arrowhead_plus.gif")
photo3=PhotoImage(file="/Users/apple/Desktop/images Tkinter/arrowhead_circle.gif")

tl.config(image=photo1,width="100",height="100")
tl.pack(side='top',anchor='nw')

tm.config(image=photo1,width="100",height="100")
tm.pack(side='top',anchor='n')

tr.config(image=photo1,width="100",height="100")
tr.pack(side='top',anchor='ne')

ml.config(image=photo1,width="100",height="100")
ml.pack(anchor='w')

mm.config(image=photo1,width="100",height="100")
mm.pack(anchor='center')

mr.config(image=photo1,width="100",height="100")
mr.pack(anchor='e')

ll.config(image=photo1,width="100",height="100")
ll.pack(anchor='sw')

lm.config(image=photo1,width="100",height="100")
lm.pack(anchor='s')

lr.config(image=photo1,width="100",height="100")
lr.pack(anchor='se')

root.mainloop()

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

我建议只使用Widget.pack()进行快速原型设计;只是为了确保一切正常。对于一个井字游戏网格非常方便。只需指定行和列,Widget.grid(row=#, column=#)更多关于TkInter的网格系统here

答案 1 :(得分:1)

不要使用pack();使用grid()。例如,button.grid(row=0, column=1)

相关问题