在Tkinter中,如何在定义内使用画布?

时间:2019-02-05 12:04:37

标签: python-3.x tkinter tkinter-canvas photoimage

在我最近在python3.3中编写的代码中,我正在尝试创建水果机,并且处于早期阶段。看来,一旦我使用“帮助”按钮,“ ianh.gif”就不会显示。

from tkinter import *

def help(event):

   global ian
   Window=Tk()
   Window.geometry('200x70')
   Window.title('Need Help With Gambling addiction?')
   canvas = Canvas(Window, width = 300, height = 300)
   canvas.pack()
   ian = PhotoImage(file = 'Images/Ianh.gif')
   canvas.create_image(20,20, anchor=NW, image=img)
   Window.mainloop()

def start_game(event):
   Window=Tk()
   Window.geometry('200x70')
   Window.title('Fruit Machine')

Window=Tk()
Window.geometry('200x70')
Window.title('Main Menu')


canvas = Canvas(Window, width = 300, height = 300)
canvas.pack()
img = PhotoImage(file = 'Images/Cherries.gif')
canvas.create_image(20,20, anchor=NW, image=img)


L1=Label(Window, text="WELCOME TO THE FRUIT MACHINE")
L1.pack()
B1=Button(Window, text="Start")
B1.bind("<Button-1>",start_game)
B1.pack()
B2=Button(Window, text="Help")
B2.bind("<Button-1>",help)
B2.pack()




Window.mainloop()

在“帮助”定义中,PhotoImage和所有画布命令似乎无法正常运行。

1 个答案:

答案 0 :(得分:0)

这应该有效:

def help(event):
   global ian
   Window=Toplevel()
   Window.geometry('200x70')
   Window.title('Need Help With Gambling addiction?')
   canvas = Canvas(Window, width = 300, height = 300)
   canvas.pack()
   ian = PhotoImage(file = 'Images/Ianh.gif')
   canvas.create_image(20,20, anchor=NW, image=ian)

多个mainloop()不能正常工作。

如果您需要更多结构,请查看this