我正在使用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()
(这些类都在同一个文件中,为了更好的可读性,我对代码进行了拆分)
这里的代码是我程序的后端,这个模块包含了我所有的pyplay代码:
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-窗口的更多信息。也许我只是不知道它应该在哪里?
但是你可以从window_name.destroy()
开始。这应该关闭给定的窗口。你似乎已经使用了这个功能,所以那应该对你有用吧?
我可以找到您实际使用的pygame
。没有使用这个库的代码行,对吗?
我在youtube上的视频中找到了一个教程,那个家伙正在运行以下代码: 如果event.type==pyplay。退出: 当我写“pygame.quit:”而不是“pygame.quit:”(在for循环中)时,窗口不会关闭。我完全是个初学者。这是我们必须大写的命令吗?有人能解释一下原因吗?
嗨,我为AWT窗口创建了这个java代码 当我按下X按钮试图关闭窗户时,什么也没发生。窗户一直开着。我对示例代码做了以下更改:公共类labelExample extends Frame implements ActionListener{ 我添加了新的awt-frame f,名称为“Label example”。 Frame f;Frame f=新框架(“Label example”);并添加了此
问题内容: 最终编辑: 我在关闭pyplot窗口这一主题上发现,实际上可能不应该使用pyplot完成它。SRK给出了一个很好的示例,说明如何处理将在下面的答案中更新的图。我也偶然发现了如何将pyplot图放到Tkinter窗口中,并且Tkinter比pyplot更擅长打开和关闭窗口。这里是怎样把一个pyplot情节变成Tk的窗口, /最终编辑 我希望能够显示多个图,然后能够从某些代码输入中分别关闭
我刚刚花了相当多的时间来寻找pygame的64位安装,以便与Python3.3一起使用(这里),现在我正在尝试创建一个窗口。然而,尽管窗口打开得很好,但当它按下x按钮时并没有关闭。事实上,我必须关闭闲置关闭窗口。我正在运行Win7的64位版本。这是我的密码: 当我附加 它仍然没有关闭。我唯一的猜测就是那个游戏。退出可能会进入其中一个循环,但即使解决了这个问题,我还是更愿意在需要时关闭窗口。
我有一个tkinter GUI python代码,它为我的代码创建了一个GUI接口,在稍后的代码中使用了声音工具包(它也使用Tk,并使用root=Tk()创建了一个实例)。因为,以前GUI应用程序的主循环已经在运行,所以每当调用snack函数时,就会弹出一个新的空默认tk窗口。由于这种情况经常发生,所以当代码执行时,屏幕上有数百个空tk窗口。我曾尝试使用多种方法关闭它们。毁灭,根。撤消、WM_删除
我试图创建一个程序,它有一个tkinter窗口打开,然后当你按下一个按钮,它关闭tkinter窗口,打开一个pyplay窗口。然而,当我点击按钮打开pyplay窗口时,它会打开pyplay窗口,而tkinter窗口保持打开状态。 代码: 我还尝试使用: 我怎样才能解决这个问题?(我正在MacOS 11.1上运行Python 3.7.7)