簡體   English   中英

如何將 tkinter window 設置為僅在用戶第一次運行程序時打開並且不再打開?

[英]How can I set a tkinter window to open only for the first time user runs the program and never open again?

我一直在使用 Python 和 tkinter 設計語音助手應用程序。 我希望用戶設置在其中搜索歌曲、電影等的默認目錄。這只發生在用戶第一次運行程序時,並且永遠不會再發生,直到他希望以后更改目錄,他將能夠使用設置按鈕。 有沒有辦法創建一個 tkinter window ,默認情況下只在用戶第一次運行程序時顯示,並且在用戶希望這樣做之前不再顯示?

您需要在文件系統上存儲某種工件來回答“這個程序以前運行過嗎?”的問題。 例如,它可以創建一個名為~/.myprogramrc的文件。 接下來,只需檢查該文件是否存在,如果該文件不存在,則打開 GUI。

class App(tk.Tk):

    def __init__(self):
        rc_file = Path.home() / ".myapprc"
        if not rc_file.exists():
            # a side effect of this should create the file .myapprc
            # in the home directory.
            config = self.prompt_for_config(rc_file)
        else:
            config = self.read_config(rc_file)

暫無
暫無

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

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