【问题标题】:Pause game shell window on tkinter and open a new shell window在 tkinter 上暂停游戏 shell 窗口并打开一个新的 shell 窗口
【发布时间】:2018-08-03 17:41:44
【问题描述】:

使用 Tkinter python。

我正在创建一个游戏,其中有一个退出按钮。我想当我点击退出按钮时我的游戏暂停并打开一个新的 shell。如果我点击 NO 按钮,那么游戏外壳将恢复工作。如果我点击Yes按钮然后退出游戏。 按钮工作正常。当我点击退出按钮时出现问题,游戏应该暂停并在点击否按钮时恢复

def quit_function():
    YES = Button(tk9,text="YES",font=("Showcard Gothic",10), command= os._exit(0), fg='white', bg='red', width=10)
    NO = Button(tk9,text="NO",font=("Showcard Gothic",10), command= tk9.destroy, fg='white', bg='red', width=10)

    #**Quit Button**
    QUIT  = Button(tk3,text="QUIT",font=("Showcard Gothic",10), command= quit_function, fg='white', bg='red', width=10)

这工作正常但不是主要问题,主要问题是我无法暂停和恢复游戏

【问题讨论】:

  • 我建议查看 tkMessageBox 包,它为您提供对话框窗口以供使用。您可以将一个事件附加到正在关闭的窗口/一个按钮,然后该按钮将显示一个对话框窗口。
  • 点击退出按钮时我想要两个选项,点击退出按钮时我的游戏应该暂停

标签: python-3.x pygame tkinter-canvas


【解决方案1】:

您可以使用游戏循环来更新您的游戏并测试它是否暂停,并且可以通过消息框完成是或否

from tkinter import *
from tkinter import messagebox

def quit_callback():
    global pause
    pause = True
    if messagebox.askokcancel("Exit", "Wanna leave?"):
        root.destroy()
    pause = False

def game_loop():
    while True:
        if not pause:
            print("game working")
        root.update()


root = Tk()

Label(root, text="your game here")
pause = False

root.protocol('WM_DELETE_WINDOW', quit_callback)
root.after(0, game_loop)
root.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-11
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 2013-09-18
    • 2015-07-28
    • 1970-01-01
    相关资源
    最近更新 更多