我已经分别编写了Tkinter窗口和我的pygame游戏。但是,当我试图将两者联系起来时,游戏将无法运作。
Tkinter窗口用于允许用户输入数据,然后将数据保存到文件中并在使用pygame进行的游戏中使用。主项目是使用pygame制作的,而Tkinter窗口是一个附加功能。
我试过这个,但界面冻结了,我收到了这个错误:
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
有没有办法在我的pygame循环中嵌入Tkinter事件循环?
N.B这是我的A-Level Computing项目的一部分,所以我非常感谢指针。
这是我的代码:
from tkinter import *
import tkinter as tk
import json
class newWords(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title("New Words")
self.resizable(width=False, height=False)
self.newWord = Label(self, text= "Add")
self.newWord.grid(row= 0, column = 0)
self.newWord_entry = Entry(self, width=20)
self.newWord_entry.grid(row = 1, column = 0)
self.add_button = Button(self, width=20, text="Add", command=self.add )
self.add_button.grid(row = 2, column = 0)
self.protocol("WM_DELETE_WINDOW", self.save)
self.mainloop()
def add(self):
global en_Words
add = self.newWord_entry.get()
if add != "":
en_Words.append(add)
self.newWord_entry.delete(0, END)
def clearBox(self):
self.new_Word_entry.delete(0, END)
return
def save(self):
with open('CUSTOM_enWords.json', 'w') as f:
json.dump(en_Words, f, indent=2)
if messagebox.askyesno("Exit", "Do you want to stop creating this custom list?"):
self.destroy()
def AddNew():
addNew = True
while addNew:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
quit()
if event.key == pygame.K_SPACE:
addNew = False
gameDisplay.fill(white)
message_to_screen("Add New Words",
black,
-200,
"large")
custom = newWords()
message_to_screen("Press SPACE to go back to Main Menu or ESC to quit.",
black,
200)
pygame.display.update()
clock.tick(FPS)