簡體   English   中英

關閉 Tkinter window 並打開另一個

[英]Closing Tkinter window and opening another

我正在嘗試構建一個簡單的劊子手游戲。 我想在開頭有一個 window 來詢問“秘密詞”。 然后我希望 window 關閉並在游戲中打開另一個 window。 如果不在一個函數中構建整個游戲,我想不出一種方法來做到這一點。 如何最好地做到這一點?

創建另一個 window? 嘗試這個:

使用多種功能

from tkinter import *
from turtle import onclick


def gameWindow(word):
    window = Tk()
    Label(text=word).pack()
    # your game code

    def anotherGuess():
        window.destroy()
        getWord()
    Button(text='another guess?', command=anotherGuess).pack()
    window.mainloop()


def getWord():
    window = Tk()
    entry = Entry(master=window)
    entry.pack()

    def onclick():
        word = entry.get()
        window.destroy()
        gameWindow(word)
    btn = Button(master=window, text="submit", command=onclick)
    btn.pack()
    window.mainloop()


def main():
    getWord()


if __name__ == '__main__':
    main()

我相信您正在尋找的是頂級Toplevel ,它的行為就像普通的根 window。 下面是一個快速而骯臟的例子:

import tkinter as tk

root = tk.Tk()
root.title("Game window")
root.iconify()#Hide from user site, still exists as a minimised window


def submitSecretWord():
    #code to collect secret word or what ever
    root.deiconify()#Make visible
    second_window.destroy()#close second window


#make toplevel window
second_window = tk.Toplevel(master=root)


#make widgets
secret_word_entry = tk.Entry(master=second_window)#Belongs to the second window

submit_button = tk.Button(master=second_window, text="Submit secret word", command=submitSecretWord) #Belongs to the second window

#pack widgets
secret_word_entry.pack(side=tk.LEFT)
submit_button.pack(side=tk.RIGHT)


root.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM