我做了一个关于猜数字的游戏。经过多次尝试,仍然无法改变一个问题。 代码如下:
#
#
import tkinter
import random
import pygame
#
pygame.init()
#
root = tkinter.Tk()
#
bg = tkinter.Canvas(root, width=1000, height=750)
bg.pack()
bg_p = tkinter.PhotoImage(file="bg.png")
bg.create_image(500, 375, image=bg_p)
#
bgm = pygame.mixer.Sound("bgm.mp3")
bgm.play(-1)
#
low = 0
high = 101
#
random.randrange(low, high)
#
def onclick():
#
global low, high
#
guess = int(input("Guess a number: "))
#
if guess == answer:
#
bg = tkinter.Canvas(root, width=1000, height=750)
bg.pack()
bg_p = tkinter.PhotoImage(file="bingo.png")
bg.create_image(500, 375, image=bg_p)
#
low = guess
high = guess
#
bingo = pygame.mixer.Sound("bingo.mp3")
bingo.play(0)
#
elif guess > answer:
#
bg = tkinter.Canvas(root, width=1000, height=750)
bg.pack()
bg_p = tkinter.PhotoImage(file="too_high.png")
bg.create_image(500, 375, image=bg_p)
#
high = guess
#
lol = pygame.mixer.Sound("lol.mp3")
lol.play(0)
#
else:
#
bg = tkinter.Canvas(root, width=1000, height=750)
bg.pack()
bg_p = tkinter.PhotoImage(file="too_low.png")
bg.create_image(500, 375, image=bg_p)
#
low = guess
#
lol = pygame.mixer.Sound("lol.mp3")
lol.play(0)
#
while True:
#
for event in pygame.event.get():
#
if event.type == pygame.QUIT:
#
pygame.quit()
exit()
#
if event.type == pygame.MOUSEBUTTONDOWN:
#
onclick()
#
try:
#
l1.destroy()
l2.destroy()
#
except:
#
pass
#
finally:
#
l1 = tkinter.Label(root, text=str(low), font=("Arial", 60), bg="white")
l1.place(x=280, y=570)
l2 = tkinter.Label(root, text=str(high), font=("Arial", 60), bg="white")
l2.place(x=630, y=570)
#
root.mainloop()
执行程序后,返回_tkinter.TclError:无法调用“label”命令:应用程序已被销毁。 无论我把 mainloop() 放在哪里,它都不起作用。 为什么?
我到处都放了mainloop(),但还是不行。
你混合了
pygame
和tkinter
,但看起来一团糟。
您尝试使用
pygame.event
但没有创建 pygame window
- 因此系统不会将事件发送到 pygame
。当您单击 pygame.MOUSEBUTTONDOWN
时,它不会生成 tkinter window
,因为只有当您单击 pygame window
时,它才会生成它。所以它无法得到pygame.QUIT
和pygame.MOUSEBUTTONDOWN
。坦率地说,如果您仅使用 pygame
来播放声音,则不需要 pygame 循环。
如果您创建了
tkinter window
那么您应该使用 tkinter event
。这意味着当您点击按钮时,使用tkinter.Button(..., command=on_click)
来执行功能。或者,当您单击 root.bind('<Button-1>', on_click)
中的任何位置时,您必须
tkinter window
才能执行功能
这是我的版本 - 但我没有测试如何工作
pygame.mixer
因为此时我的 Linux 上出现音频问题。
import tkinter
import random
import pygame
# --- functions ---
def on_click(event=None): # default value for event because `command=` doesn't send event
global low, high
guess = int(entry.get()) # entry needs `.get()` to get value
if guess == answer:
bg.itemconfig(image_id, image=bg_p_bingo) # replace image in PhotoImage
low = guess
high = guess
label_low['text'] = str(low) # replace text in Label
label_high['text'] = str(high) # replace text in Label
#bingo = pygame.mixer.Sound("bingo.mp3")
#bingo.play(0)
elif guess > answer:
bg.itemconfig(image_id, image=bg_p_high) # replace image in PhotoImage
high = guess
label_high['text'] = str(high) # replace text in Label
#lol = pygame.mixer.Sound("lol.mp3")
#lol.play(0)
else:
bg.itemconfig(image_id, image=bg_p_low) # replace image in PhotoImage
low = guess
label_low['text'] = str(low) # replace text in Label
#lol = pygame.mixer.Sound("lol.mp3")
#lol.play(0)
# --- main ---
low = 0
high = 101
# ---
answer = random.randrange(low, high)
#pygame.mixer.init()
root = tkinter.Tk()
label = tkinter.Label(root, text="Guess a number: ")
label.pack(padx=5, pady=5)
entry = tkinter.Entry(root)
entry.pack(padx=5, pady=5)
button = tkinter.Button(root, text='CHECK', command=on_click)
button.pack(padx=5, pady=5)
entry.bind('<Return>', on_click) # run function on pressing ENTER - it sends `event` to function
bg = tkinter.Canvas(root, width=300, height=300)
bg.pack()
bg_p = tkinter.PhotoImage(file="bg.png")
bg_p_bingo = tkinter.PhotoImage(file="bingo.png")
bg_p_low = tkinter.PhotoImage(file="too_low.png")
bg_p_high = tkinter.PhotoImage(file="too_high.png")
image_id = bg.create_image(150, 150, image=bg_p, anchor='center')
frame = tkinter.Frame(root) # create Frame to put widgets in one row
frame.pack(fill='both', expand=True)
label_low = tkinter.Label(frame, text=f'{low}', font=("Arial", 30), bg="white", width=3)
label_low.pack(side='left', padx=5, pady=5, fill='x', expand=True)
label_high = tkinter.Label(frame, text=f'{high}', font=("Arial", 30), bg="white", width=3)
label_high.pack(side='right', padx=5, pady=5, fill='x', expand=True)
#bgm = pygame.mixer.Sound("bgm.mp3")
#bgm.play(-1)
root.mainloop()
pygame.mixer.quit()