簡體   English   中英

python tkinter 上的 function 問題

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM