Python Tkinter从另一个文件中的类调用方法,并刷新画布

时间:2016-04-26 11:44:21

标签: python tkinter

我正在使用Canvas创建一个使用Tkinter的简单状态游戏。 当用户点击按钮getIn时,man的值应该改变,但是,它并没有。我曾尝试使用self.canvas.update_idletasks,但我也不太确定如何制作这项工作。 以下是当用户点击" getIn":

时应该运行的功能
def manGetIn():
    man = w.create_oval(350, 250, 205, 210, fill="black")
    w.Canvas.update_idletasks
    #Hvilken side er båten på? Fuark.

这里是游戏相关GUI的GUI

    from Tkinter import *
    from tape import * #The module with methods I want to call in the game



class RiverCrossing:
    def __init__(self, master):
        self.tape = Database()



        master.title("River Crossing")
        w = Canvas(master, width=900, height=900)
        w.pack()

        w.create_rectangle(200, 900, 750, 0,  fill="blue")
        w.create_rectangle(200, 900, 0, 0, fill="green")
        w.create_rectangle(900, 0, 750, 800, fill="green")
        #Lager landskapet(water, left, right)

        global boat 
        boat = w.create_rectangle(400, 300, 201, 200, fill="brown") 
        global chicken
        chicken = w.create_oval(150, 180, 180, 200, fill="yellow") 
        global grain 
        grain = w.create_oval(130, 180, 160, 170, fill="white") 
        global man 
        man = w.create_oval(150, 250, 180, 200, fill="black") 
        global fox 
        fox = w.create_oval(100, 130, 180, 150, fill="orange")  



        bottomFrame = Frame(master)
        bottomFrame.pack(side=BOTTOM)

        getIn = Button(bottomFrame, text="Get in boat", bg="white", fg="black", command= lambda: Database.manGetIn)

        crossRiver = Button(bottomFrame, text="Cross River", bg="black", fg="white", command= lambda: Database.set_boat(new_pos)) 



        getIn.grid(row=0)
        crossRiver.grid(row=2, column=2)



root = Tk()
rc = RiverCrossing(root)
root.mainloop()

当我测试游戏并调用riverCrossing函数(来自 tape.py 模块)时,我收到错误

  

未定义全局new_pos。

  def set_boat(self, new_pos):
    if new_pos in ('left','right'):
        self.boat = new_pos
        return True
    else:
        return False

0 个答案:

没有答案