我正在同时使用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()
MainMenu
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)
The PauseMenu
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)
答案 0 :(得分:0)
有关何时应该发生以及您想关闭哪个Tkinter窗口的更多信息。也许我只是无法发现应该在哪里?
但是您可以从window_name.destroy()
开始。那应该关闭给定的窗口。
您似乎已经在使用该功能,那么应该对您有用吗?
我可以找到您实际使用pygame
的位置。使用该库没有代码行吗?