所以,我正在使用 tkinter,并且需要一個彈出的頂層視窗來詢問用戶他們想要使用什么資料庫。但是,我似乎無法在啟動時自動激活頂層。如果我呼叫一個在呼叫 root.mainloop() 之后會立即打開頂層的函式,則不會打開任何東西。
def chooseDatabase():
top = Toplevel(root)
choices = db.getDatabases()
choices_to_string = []
for c in choices:
choices_to_string.append(c.getName())
variable = StringVar()
variable.set(choices_to_string[0])
option = OptionMenu(top, variable, *choices_to_string, command=lambda x:[setDatabaseToUse(variable.get()), top.destroy()])
option.pack()
top.mainloop()
def main():
updateList()
root.mainloop()
chooseDatabase()
但是,如果我將 chooseDatabase() 函式系結到一個按鈕,它會在啟動后按下按鈕時愉快地打開頂層。
是什么導致了這種情況,我該如何規避它?
uj5u.com熱心網友回復:
關閉應用程式后root.mainloop執行的操作(有關更多資訊,請參見此處)。您應該嘗試使用root.after,以便您chooseDatabase在應用啟動后的一個非常小的延遲后安排您的意圖運行。
def main():
updateList()
root.after(50, chooseDatabase)
root.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/492005.html
上一篇:如何在Pythontkinter中自定義復選框、下拉串列等的位置?
下一篇:如何洗掉tkinter單選按鈕?
