Python GUI 在移动时崩溃

Python GUI crasing when moved

tl;dr : 为什么我的 GUI 在移动时崩溃。

嗨,首先,是的,我在这里和 google 上查找了一些类似的 post,我可能错过了金苹果所以如果这个问题真的得到了回答 40次,那我真的很抱歉。

所以我正在使用 Python,我的脚本基本上是两个循环,一个小循环,您所在的地方 "stuck",直到按下一个键,一个更大的循环包含所有实际的好东西。

我的问题是 GUI,一切正常,问题是当您与 windows 交互时它崩溃了。我试着看一下线程,但我没能让它像我想要的那样工作。由于脚本基本上是 "while 1:" 我可以理解 GUI 不喜欢它...

GUI 本身仅用作输出,不需要按钮,我不需要很大的刷新率。

如果需要,我愿意更改我的大部分代码,我仍处于早期开发阶段。特别是放弃 Tkinter,我 "almost" 确定我可以用 PyGame 做我想做的事,但这对我来说似乎有点过分了。

下面的代码去掉了所有不必要的代码,只是准系统,但如果这个 GUI 没有崩溃,它应该适用于我的完整脚本。

import keyboard as kb
from tkinter import *
# Much more imports are needed for the full script
# Here I also define a lot of fonction use in the loop


def talk(string: str):
    """
    Update the UI witht he new text to output
    :param string: the strind to output
    :return: None
    """
    canvas.create_polygon([0, 0, 0, 125, 600, 125, 600, 0], fill="black", width=2)
    canvas.create_text(300, 100, text=string, font="terminal 20", fill="white")
    canvas.pack()
    root.update()


# Creating the ui
root = Tk()
canvas = Canvas(root, width=600, height=250, background='black')
canvas.create_line(50, 200, 550, 200, fill="red", width=3)
canvas.pack()
root.update()

Stop = False
while not Stop:                     # I do have way to stop this loop and shutdown the script properly
    PushToTalk = False
    talk("")                        # Reseting the output text
    while not PushToTalk:           # Stuck here until "pause" key is press
        if kb.is_pressed("pause"):
            PushToTalk = True

    talk("Yes ?")
    # Here are a lot of stuff happening
    # And the "talk" function is use a lot for upputing informations
print("Bye")

希望你能帮助我!

吕希里翁。

由于内部 while 循环会阻止 window 的更新,您可以在内部 while 循环的末尾添加 root.update(),如下所示:

Stop = False
while not Stop:                     # I do have way to stop this loop and shutdown the script properly
    PushToTalk = False
    talk("")                        # Reseting the output text
    while not PushToTalk:           # Stuck here until "pause" key is press
        if kb.is_pressed("pause"):
            PushToTalk = True
        root.update()  # let tkinter handle update    
    talk("Yes ?")
    # Here are a lot of stuff happening
    # And the "talk" function is use a lot for upputing informations
print("Bye")

但是,在主线程中使用 while 循环并不是一个好习惯。最好将上面的代码块放在一个线程中:

import keyboard as kb
from tkinter import *
import threading
# Much more imports are needed for the full script
# Here I also define a lot of fonction use in the loop


def talk(string: str):
    """
    Update the UI witht he new text to output
    :param string: the strind to output
    :return: None
    """
    canvas.itemconfig(msg, text=string)


# Creating the ui
root = Tk()
canvas = Canvas(root, width=600, height=250, background='black')
canvas.create_line(50, 200, 550, 200, fill="red", width=3)
msg = canvas.create_text(300, 100, text='welcome', font="terminal 20", fill="white")
canvas.pack()

Stop = False

def chat():
    while not Stop:
        talk("")
        while not kb.is_pressed("pause"): pass
        talk("Yes ?")
        # Here are a lot of stuff happening
        # And the "talk" function is use a lot for upputing informations
    print("Bye")

threading.Thread(target=chat, daemon=True).start()
root.mainloop()