【问题标题】:Python 3 Tkinter : Popup not displayingPython 3 Tkinter:弹出窗口不显示
【发布时间】:2020-06-04 13:08:20
【问题描述】:

我有两个由同一个按钮触发的弹出窗口:一个有一个进度条、一个标签和一个按钮。另一个只有 2 个标签和一个按钮。如果我评论进度条的更新功能:不会显示任何弹出窗口。如果我尝试只显示第二个(没有进度条)它不会显示。我只能同时显示两个,我只想显示第二个(没有进度条)

这是第一个弹出窗口的代码:

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)

我在循环开始之前调用该函数,然后更新同一循环内的值:

更新:

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”...函数,屏幕上什么也没有显示,如果我只尝试显示第二个,也没有任何显示。我确定这是一个愚蠢的问题,但我正在努力解决这个问题......

【问题讨论】:

  • Tk() 的多个实例会导致各种问题。尝试使用 Toplevel() 代替弹出窗口 - 这样,所有内容都存在于单个 Tk 实例中,单个 .update()(或正在运行的 .mainloop())将处理所有窗口的事件。
  • 我尝试使用 Toplevel,但不幸的是它并没有改变任何东西..

标签: python python-3.x tkinter


【解决方案1】:

您的代码没有问题,但请尝试删除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 中可以避免这种情况。

这仅对某些版本有效。也许您正在使用的就是其中之一。否则我不知道。

【讨论】:

  • 另一种方法: 1. 在def destroy_popup 中,将self.popup_process.destroy 放入if self.destroy:; 2. 在添加按钮之前将self.destroy 放在False 上; 3.打包按钮后将self.destroy放在Trueself.destroy 放在False 禁用销毁窗口,并将其放在True 允许在单击按钮时销毁。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-12
相关资源
最近更新 更多