簡體   English   中英

Python 3 Tkinter:彈出窗口不顯示

[英]Python 3 Tkinter : Popup not displaying

我有兩個由同一個按鈕觸發的彈出窗口:一個有一個進度條,一個 label 和一個按鈕。 另一個只有 2 個標簽和一個按鈕。 如果我評論進度條的更新 function :兩個彈出窗口都不顯示。 如果我嘗試只顯示第二個(沒有進度條)它不會顯示。 我只能同時顯示兩個,我只想顯示第二個(沒有進度條)

這是第一個彈出窗口的代碼:

def sim_process_bar(self):
        self.popup_process = Tk()
        self.popup_process.wm_title("Simulation process...")
        self.progressBar = ttk.Progressbar(self.popup_process, orient = 'horizontal', length = 286, mode = 'determinate')
        self.progressBar['maximum'] = 100
        self.stopBtn = Button(self.popup_process, text="Stop Simulation", command = self.popup_process.destroy)
        self.progressBar.grid(column = 1, row = 1, pady = 10)
        self.stopBtn.grid(column = 1, row = 2)

        self.labelPercentage = Label(self.popup_process, text="Percentage complete : 0% ")
        self.labelPercentage.grid(column = 1, row = 3, pady = 10)

    def update_value_process_bar(self, value):
        self.progressBar['value'] = value
        self.progressBar.update()

第二個彈窗的代碼:

def process_feedback(self):
        self.popupFeedback = Tk()
        self.popupFeedback.wm_title("Simulation process...")
        self.labelTimestep = Label(self.popupFeedback, text="Current timestep : ")
        self.labelTimestep.grid(column = 0 , row = 1, pady = 10)

        self.labelPercentage2 = Label(self.popupFeedback, text="Percentage complete : ")
        self.labelPercentage2.grid(column = 0, row = 2, pady = 10)

        self.Btnstop = Button(self.popupFeedback, text="Stop Simulation", command = self.popupFeedback.destroy)
        self.Btnstop.grid(column = 0, row = 3)

我在循環開始之前調用 function,然后在同一個循環中更新值:

更新:

if(round(k/self.total_timestep_of_simulation*100,1).is_integer()):
    self.update_value_process_bar(k/self.total_timestep_of_simulation*100)
    self.labelPercentage['text'] = "Percentage complete : " + str(round(k/self.total_timestep_of_simulation*100,1)) + "%"
#UPDATE POPUP FEEDBACK
self.labelTimestep['text'] = "Current timestep : " + str(data_update.strftime("%b %d %Y %H:%M:%S"))
self.labelPercentage2['text'] = "Percentage complete : " + str(round(k/self.total_timestep_of_simulation*100,1)) + "%"

所以如果我評論“self.update_value”... function,屏幕上什么也沒有顯示,如果我只嘗試顯示第二個,也沒有任何顯示。 我確定這是一個愚蠢的問題,但我正在努力解決這個問題......

您的代碼沒問題,但嘗試刪除self.stopBtn = Button(self...行或將其替換為

    self.stopButton = Button(self.popup_process,
                             text="Stop Simulation",
                             command = self.destroy_popup)

並添加

def destroy_popup(self):
    self.popup_process.destroy()

另一個彈出窗口也一樣:

    self.Btnstop = Button(self.popupFeedback,
                          text="Stop Simulation",
                          command = self.destroy_popup)
    ...

def destroy_popup(self):
    self.popupFeedback.destroy()

有時按鈕“建立連接”到它所連接的控件。 在這個例子中, self.popup_process.destroy是一個包含在Tkinter模塊中的循環。 所以按鈕執行該命令。 把它放在def中可以避免這種情況。

這僅對某些版本有效。 也許您正在使用的就是其中之一。 否則我不知道。

暫無
暫無

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

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