【问题标题】:In Tkinter how do you use canvas inside a defintion?在 Tkinter 中,如何在定义中使用画布?
【发布时间】:2019-02-05 12:04:36
【问题描述】:

在我最近用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 和所有画布命令似乎无法正常运行。

【问题讨论】:

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


    【解决方案1】:

    这应该可行:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-02
      • 2015-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多