Python 更改为pygame窗口时,如何关闭tkinter窗口?

Python 更改为pygame窗口时,如何关闭tkinter窗口?,python,tkinter,pygame,Python,Tkinter,Pygame,我正在使用tkinter和pygame创建一个游戏,当程序从菜单启动pygame时,我不知道如何关闭tkinter窗口。谢谢你的帮助 # Importing Widgets and from different modules: import Customised_Widgets as cw # Importing the libraries required to operate the program: import pygame as py import tkinter as tk fro

我正在使用tkinter和pygame创建一个游戏,当程序从菜单启动pygame时,我不知道如何关闭tkinter窗口。谢谢你的帮助

# Importing Widgets and from different modules:
import Customised_Widgets as cw
# Importing the libraries required to operate the program:
import pygame as py
import tkinter as tk
from tkinter import *
import DOG_BackEnd as be

LARGE_FONT = ("Comic Sans MS", 20) # Font for large text selected

# Classes that help create all the menus using tkinter


class Dog(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.iconbitmap(self, default="N:/ICT/A levels/Year 13/NEA/Game Files/D.O.G.ico")
        tk.Tk.wm_title(self, "D.O.G.")
        tk.Tk.wm_geometry(self, "960x720")
        tk.Tk.wm_resizable(self, False, False)

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        """self.image = tk.PhotoImage(file="N:/ICT/A levels/Year 13/NEA/Game Files/Fonts/Menu Font.png")
        self.image2 = tk.PhotoImage(file="N:/ICT/A levels/Year 13/NEA/Game Files/Fonts/Options Font.png")
        self.image3 = tk.PhotoImage(file="N:/ICT/A levels/Year 13/NEA/Game Files/Fonts/Paused Font.png")"""

        self.frames = {}

        for F in (MainMenu, PauseMenu, OptionsMenu):
            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(MainMenu)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()
主菜单

class MainMenu(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        tk.Frame.configure(self, background="#99D9EA")

        self.menu_image = tk.PhotoImage(file="N:/ICT/A levels/Year 13/NEA/Project Design/Slide1.png")

        tk.Label(self, image=self.menu_image).place(x=0, y=0)

        button1 = cw.Button(self, default="N:/ICT/A levels/Year 13/NEA/Game Files/Buttons/Main Menu/Play Button.gif",
                            hover="N:/ICT/A levels/Year 13/NEA/Game Files/Buttons/Main Menu/Play Button 2.gif",
                            highlightthickness=0, highlightbackground="#99D9EA", bd=0, borderwidth=0, activebackground="#99D9EA", command=be.Game)
        button1.place(x=660, y=215)

        button2 = cw.Button(self, default="N:/ICT/A levels/Year 13/NEA/Game Files/Buttons/Main Menu/Options Button.gif",
                            hover="N:/ICT/A levels/Year 13/NEA/Game Files/Buttons/Main Menu/Options Button 2.gif",
                            highlightbackground="#99D9EA", activebackground="#99D9EA", bd=0, borderwidth=0, highlightthickness=0, command=lambda: controller.show_frame(OptionsMenu))
        button2.place(x=660, y=293)

        button3 = cw.Button(self, default="N:/ICT/A levels/Year 13/NEA/Game Files/Buttons/Main Menu/Exit Button.gif",
                            hover="N:/ICT/A levels/Year 13/NEA/Game Files/Buttons/Main Menu/Exit Button 2.gif",
                            highlightthickness=0, highlightbackground="#99D9EA", borderwidth=0, bd=0, command=lambda: controller.destroy())
        button3.place(x=660, y=366)
选项菜单

class OptionsMenu(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        tk.Frame.configure(self, background="#99D9EA")

        self.menu_image = tk.PhotoImage(file="N:/ICT/A levels/Year 13/NEA/Project Design/Slide2.png")

        tk.Label(self, image=self.menu_image).place(x=0, y=0)

        button1 = cw.Button(self, default="N:/ICT/A levels/Year 13/NEA/Game Files/Buttons/Options Menu/Music Button.gif",
                            hover="N:/ICT/A levels/Year 13/NEA/Game Files/Buttons/Options Menu/Music Button 2.gif",
                            highlightthickness=0, highlightbackground="#99D9EA", bd=0, borderwidth=0, activebackground="#99D9EA", command=lambda: controller.show_frame(OptionsMenu))
        button1.place(x=414, y=129)

        button2 = cw.Button(self, default="N:/ICT/A levels/Year 13/NEA/Game Files/Buttons/Options Menu/SFX Button.gif",
                            hover="N:/ICT/A levels/Year 13/NEA/Game Files/Buttons/Options Menu/SFX Button 2.gif",
                            highlightthickness=0, highlightbackground="#99D9EA", bd=0, borderwidth=0, activebackground="#99D9EA", command=lambda: controller.show_frame(OptionsMenu))
        button2.place(x=414, y=210)

        button3 = cw.Button(self, default="N:/ICT/A levels/Year 13/NEA/Game Files/Buttons/Options Menu/Location Button.gif",
                            hover="N:/ICT/A levels/Year 13/NEA/Game Files/Buttons/Options Menu/Location Button 2.gif",
                            highlightthickness=0, highlightbackground="#99D9EA", bd=0, borderwidth=0, activebackground="#99D9EA", command=lambda: controller.show_frame(OptionsMenu))
        button3.place(x=414, y=291)

        button4 = cw.Button(self, default="N:/ICT/A levels/Year 13/NEA/Game Files/Buttons/Options Menu/Exit Button.gif",
                            hover="N:/ICT/A levels/Year 13/NEA/Game Files/Buttons/Options Menu/Exit Button 2.gif",
                            activebackground="#EED9EA", highlightthickness=0, highlightbackground="#99D9EA", borderwidth=0, bd=0, command=lambda: controller.show_frame(MainMenu))
        button4.place(x=414, y=371)
波塞门努酒店

class PauseMenu(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        tk.Frame.configure(self, background="#99D9EA")

        self.menu_image = tk.PhotoImage(file="N:/ICT/A levels/Year 13/NEA/Project Design/Slide3.png")

        tk.Label(self, image=self.menu_image).place(x=0, y=0)

        button1 = cw.Button(self, text="-", command=lambda: controller.show_frame(MainMenu))
        button1.pack()


if __name__ == "__main__":
    root = Dog()
    root.mainloop()
(这些类都在同一个文件中,为了更好的可读性,我对代码进行了拆分)

这里的代码是我程序的后端,此模块包含我的所有pygame代码:

import DOG_FrontEnd
import pygame as py, sys
from pygame.locals import *


displayw = 960
displayh = 720

py.display.set_caption("Don't Over Grill (D.O.G)")

screen = py.display.set_mode((displayw, displayh), 0, 32)

clock = py.time.Clock()

player_sprite = py.image.load("N:/ICT/A levels/Year 13/NEA/Game 
Files/Sprites/Ishmael/Running/Run 1.png")
player_sprite2 = py.image.load("N:/ICT/A levels/Year 13/NEA/Game 
Files/Sprites/Shiba/Stationary 1.png")
background = py.image.load("N:/ICT/A levels/Year 13/NEA/Game Files/Map/Desert Map 1.png")
icon = py.image.load("N:/ICT/A levels/Year 13/NEA/Game Files/D.O.G.ico")

py.display.set_icon(icon)


class Game:
    py.init()

    def __init__(self, *args, **kwargs):
        self.done = False
        self.close()

    def close(self):
        while not self.done:
            screen.blit(background, (0, 0))
            screen.blit(player_sprite, (20, 450))
            for event in py.event.get():
                if event.type == py.QUIT:
                    self.done = True

                py.display.flip()

        py.display.update()
        clock.tick(60)
这里的代码是我的类,用于为tkinter窗口导入自定义按钮:

import tkinter as tk


class Button(tk.Button):
    def __init__(self, parent, default="", hover="", **kwargs):
        tk.Button.__init__(self, master=parent, **kwargs)
        self.default = tk.PhotoImage(file=default)
        self.hover = tk.PhotoImage(file=hover)
        self.configure(image=self.default)
        self.bind("<Leave>", self.on_leave)
        self.bind("<Enter>", self.on_enter)

    def on_leave(self, event):
        self.configure(image=self.default)

    def on_enter(self, event):
        self.configure(image=self.hover)
将tkinter作为tk导入
类按钮(tk.按钮):
def uuu init uuuu(self,parent,default=“”,hover=“”,**kwargs):
tk.按钮。\uuuu初始化(self,master=parent,**kwargs)
self.default=tk.PhotoImage(file=default)
self.hover=tk.PhotoImage(file=hover)
self.configure(image=self.default)
self.bind(“,self.on_-leave”)
self.bind(“,self.on_输入)
def休假(自我、事件):
self.configure(image=self.default)
def on_enter(自我,事件):
self.configure(image=self.hover)

关于何时应该发生这种情况以及要关闭哪个Tkinter窗口的更多信息。也许我就是找不到它应该在哪里

但是您可以从
窗口\u name.destroy()
开始。这将关闭给定的窗口。 你似乎已经在使用这个功能了,那么这应该对你有用吗


我可以找到您实际使用的
pygame
。没有使用此库的代码行,对吗?

很抱歉,我没有包括后端,因为我认为没有必要包括后端,因为我需要的帮助是在程序启动pygame窗口时关闭tkinter窗口,但我将进行编辑以包括后端。谢谢你的帮助。