[英]problem with a function on python tkinter
我正在使用 tkinter 进行游戏,我需要允许玩家使用 x、y 和方向的信息放置建筑物。 我创建了这个 function:
def pageChoice():
fenetre = Tk()
fenetre['bg']='white'
fenetre.title("Choose the position of your building")
fenetre.geometry("500x600")
Frame1 = Frame(fenetre, borderwidth=2, relief=GROOVE)
Label(Frame1, text="choose the orientation of your building").pack(padx=10, pady=10)
Frame1.pack(padx=30, pady=30)
value=StringVar()
orientation = IntVar()
radiobutton_1 = Radiobutton(Frame1, text="Horizontal", variable=orientation, value=1)
radiobutton_1.pack()
radiobutton_2 = Radiobutton(Frame1, text="Vertical", variable=orientation, value=2)
radiobutton_2.pack()
Frame2 = Frame(fenetre, borderwidth=2, relief=GROOVE)
Label(Frame2, text="type the coordinates of the center of your building").pack(padx=5, pady=5)
Frame2.pack(padx=10, pady=10)
Frame3 = Frame(Frame2, borderwidth=2, relief=GROOVE)
Frame3['bg']='green'
Label(Frame3, text="as x").pack(padx=10, pady=10)
Frame3.pack(side=LEFT, padx=10, pady=30)
Frame4 = Frame(Frame2, borderwidth=2, relief=GROOVE)
Frame4['bg']='green'
Label(Frame4, text="as y").pack(padx=10, pady=10)
Frame4.pack(side=RIGHT, padx=10, pady=30)
entree1 = Text(Frame3, width=10, height=1)
entree1.pack()
entree2 = Text(Frame4, width=10, height=1)
entree2.pack()
def printInput():
ord = entree1.get(1.0, "end-1c")
abs = entree2.get(1.0, "end-1c")
print(ord, abs)
ori = orientation.get()
if ori == 1:
print('horizontal')
elif ori == 2:
print('vertical')
print(ori)
bouton = Button(fenetre, text="validate", relief = RAISED, command=printInput)
bouton.pack()
fenetre.mainloop()
print(pageChoix())
当我单独运行它时它可以工作,但是当我用另一个 function 调用它时,单选按钮不起作用,我无法获得有关方向的信息:
MainJoueur1 = ['house','pool']
def Installation(L): # L is the list of buildings in inventory
fenetre = Tk()
fenetre.title("choose a building to place")
fenetre.geometry("140x90")
for i in range(len(L)):
bi = Button(fenetre, text =L[i], relief=RAISED, command = pageChoix).pack()
fenetre.mainloop()
print(Installation(MainJoueur1))
不知道问题出在哪里,谢谢帮助!
当所有 tkinter 变量都正确链接时,调用Tk()
不是问题。 问题是您的IntVar
(名为orientation
)是在tcl
实例中创建的,并且在另一个实例中不存在,因此另一个tcl
实例会混淆。 如果换行:
orientation = IntVar()
至:
orientation = IntVar(fenetre)
它应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.