如何将 Tkinter 窗口嵌入到 pygame 游戏 GUI 中? [英] How to embed a Tkinter window into a pygame game GUI?

查看:186
本文介绍了如何将 Tkinter 窗口嵌入到 pygame 游戏 GUI 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我分别对 Tkinter 窗口和我的 pygame 游戏进行了编码.但是,当我尝试将两者联系起来时,游戏将不再起作用.

I have coded the Tkinter window and my pygame game separately. However, when I tried to link the two the game would no longer work.

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

The Tkinter window is used to allow the user to input data which is then saved to a file and used in the game made using pygame. The main project is made using pygame and the Tkinter window is an added functionality.

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

I have attempted this, but the interface freezes and I get this error:

进程以退出代码 134 结束(被信号 6 中断:SIGABRT)

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

Is there a way to embed the Tkinter event loop within my pygame loop?

注意,这是我的 A-Level Computing 项目的一部分,所以我非常感谢您的指点.

N.B This is part of my A-Level Computing project, and so I would really appreciate pointers.

这是我的代码:

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)

推荐答案

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

I don't believe you can embed tkinter in to pygame.

当我需要向 pygame 游戏提供用户输入时,我使用了 PGU.

When I needed to provide user input in to a pygame game, I used the widgets available with PGU.

这篇关于如何将 Tkinter 窗口嵌入到 pygame 游戏 GUI 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆