【问题标题】:Tkinter: GUI won't appear because of loopsTkinter:由于循环,GUI不会出现
【发布时间】:2020-10-15 12:22:44
【问题描述】:

我的 Tkinter GUI 不会因为循环而出现,我怎样才能在不干扰循环流的情况下使 GUI 出现?这是代码。

for record in data:

        # The rest of the code...
        
        # Datetime and auto validation for web automation 
        while True:
            convert_time_record = datetime.datetime.strptime(record[2], '%H:%M')
            date_now = datetime.datetime.now()
            time_val = record[0] == date_now.strftime('%A') and convert_time_record.strftime('%H:%M:%S') == date_now.strftime('%H:%M:%S')
            if time_val and record[3] == "Yes" and record[4] == "Link":
                print("time true")
                chrome.open(record[5])
                time.sleep(5)
                os.system("taskkill /im chrome.exe /f")
                break
        # Check if the method was by meeting ID
            elif time_val and record[3] == "Yes" and record[4] == "Meeting ID":
                # Open Zoom 
                subprocess.call("C:\\Users\\bryan\\AppData\\Roaming\\Zoom\\bin\\Zoom.exe")
                time.sleep(3)
                # Locate the center of the join button then move the cursor
                Click('join_button.png')
                time.sleep(3)
                # Write the meeting id to the text field
                pyautogui.write(record[5])
                # Press the enter key
                pyautogui.press('enter')
                time.sleep(3)
                # Write the passcode to the text field
                pyautogui.write(record[6])
                # Press the enter key
                pyautogui.press('enter')
                time.sleep(8)
                join_computer_audio_btn = pyautogui.locateCenterOnScreen('join_audio.png')
                pyautogui.moveTo(join_computer_audio_btn)
                pyautogui.click()
    
    root.mainloop()

我尝试将root.mainloop() 放在 for 循环和 while 循环中,但这会使循环无法执行函数。

【问题讨论】:

  • 你的tk代码在哪里?
  • 别担心,它在代码的顶部,但我不想显示我所有的代码
  • 试着在循环结束时说root.update(),由于time.sleep(),GUI仍然会冻结,请正确使用root.after(),这样GUI就不会释放。

标签: python python-3.x tkinter


【解决方案1】:

您应该只运行一次root.mainloop()。 使用循环可以防止 GUI 更新,因此考虑使用不同的线程来运行循环并将更新从另一个线程发送到 GUI,或者使用对象来维护循环的状态并使用非阻塞函数来获取状态对象并使用 tkinter 的 after 函数更新和重新设置调用。

【讨论】:

  • 我明白你的意思,但我以前从未使用过线程。你能展示一个简单的代码示例吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多