Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

python 3.x - How to embed a Tkinter window into a pygame game GUI?

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.

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:

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

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

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

Here is my code:

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...