【问题标题】:How to embed a Tkinter window into a pygame game GUI?如何将 Tkinter 窗口嵌入到 pygame 游戏 GUI 中?
【发布时间】:2017-03-21 15:44:05
【问题描述】:

我已经分别编写了 Tkinter 窗口和我的 pygame 游戏。但是,当我尝试将两者联系起来时,游戏将不再运行。

Tkinter 窗口用于允许用户输入数据,然后将其保存到文件中并用于使用 pygame 制作的游戏。主项目是使用 pygame 制作的,Tkinter 窗口是一个附加功能。

我已经尝试过了,但是界面冻结并出现此错误:

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

有没有办法在我的 pygame 循环中嵌入 Tkinter 事件循环?

注意,这是我的 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)

【问题讨论】:

  • 你尝试过做什么?你没有给我们任何代码,所以我们不知道你做错了什么。

标签: python-3.x tkinter pygame


【解决方案1】:

我不相信你可以将 tkinter 嵌入到 pygame 中。

当我需要在 pygame 游戏中提供用户输入时,我使用了 PGU 提供的小部件。

【讨论】:

    猜你喜欢
    • 2014-06-12
    • 2019-09-09
    相关资源
    最近更新 更多