简体   繁体   English

tkinter,除非添加了某些功能,否则图像不会显示

[英]tkinter, Image does not show up unless some function added

I have written a code for a basic game but the image and shape don't show up unless I add something like item.pack() or win.mainloop() [which doesn't really make sense] but then the lines below it don't run. 我已经编写了一个基本游戏的代码但是图像和形状没有显示,除非我添加像item.pack()或win.mainloop()[这真的没有意义],但接下来它的下面的行不要跑。

When I don't have anything, the buttons show up but the image doesn't show up. 当我什么都没有时,按钮会显示,但图像不会显示。

import tkinter as tk
import random
from tkinter import messagebox

win = tk.Tk()

my_label = tk.Label(win, text="Color of the Baloon Game")
my_label.pack()

my_canvas = tk.Canvas(win, width=400, height=600)
my_canvas.pack()

background_image=tk.PhotoImage(file = "CS_Game_menu.png")
background_label = tk.Label(my_canvas, image=background_image)
background_label.photo = background_image
background_label.grid(row = 0, rowspan = 10, column = 0, columnspan = 10)


def drawCircle():
    color = "green"
    x1 = 265
    y1 = 80
    diameter = 90
    my_canvas.destroy()
    circle_button.destroy()
    quit_button.destroy()
    my_label.destroy()
    my_label1 = tk.Label(win, text="What is the Color of the Baloon?", font="Purisa")
    my_label1.pack()
    my_canvas1 = tk.Canvas(win, width=400, height=600)
    my_canvas1.pack()
    image1 = r"CS_Game_baloon.png"
    photo1 = tk.PhotoImage(file=image1)
    item = my_canvas1.create_image(200, 350, image=photo1)
    shape = my_canvas1.create_oval(x1, y1, x1 + diameter, y1 + diameter+20, fill=color)
    item.pack()
    game1_button = tk.Button(my_canvas1, text = "Green")
    game1_button.grid(row= 8, column = 3)
    game1_button["command"] = lambda: messagebox.showinfo("Congratulations!", "Correct Answer!")
    game2_button = tk.Button(my_canvas1, text = "Blue")
    game2_button.grid(row= 8, column = 5)
    game2_button["command"] = lambda: messagebox.showinfo("Sorry!", "Incorrect Answer!")
    game3_button = tk.Button(my_canvas1, text = "Red")
    game3_button.grid(row= 8, column = 7)
    game3_button["command"] = lambda: messagebox.showinfo("Sorry", "Incorrect Answer!")


circle_button = tk.Button(win, text="New Game")
circle_button.pack()
circle_button["command"] = drawCircle

quit_button = tk.Button(win, text="Quit")
quit_button.pack()
quit_button['command'] = win.destroy

You are using both the create_... methods and grid methods on your canvas object. 您在canvas对象上使用create_...方法和grid方法。 It won't behave as you expected. 它不会像你期望的那样表现。

To achieve what you want, you can create a Frame , put your buttons in it, and then use create_window method on your canvas: 要实现您想要的效果,您可以创建一个Frame ,将按钮放入其中,然后在画布上使用create_window方法:

def drawCircle():
    ...
    shape = my_canvas1.create_oval(x1, y1, x1 + diameter, y1 + diameter+20, fill=color)
    frame = tk.Frame(my_canvas1)
    game1_button = tk.Button(frame, text = "Green")
    game1_button.grid(row= 8, column = 3)
    game1_button["command"] = lambda: messagebox.showinfo("Congratulations!", "Correct Answer!")
    game2_button = tk.Button(frame, text = "Blue")
    game2_button.grid(row= 8, column = 5)
    game2_button["command"] = lambda: messagebox.showinfo("Sorry!", "Incorrect Answer!")
    game3_button = tk.Button(frame, text = "Red")
    game3_button.grid(row= 8, column = 7)
    game3_button["command"] = lambda: messagebox.showinfo("Sorry", "Incorrect Answer!")
    my_canvas1.create_window(200,500,window=frame)

And of course, add win.mainloop() to the bottom of your program if you haven't already. 当然,如果您还没有将win.mainloop()添加到程序的底部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM