移动时会产生Python GUI裂纹

时间:2019-02-27 20:47:57

标签: python loops tkinter crash

tl; dr:为什么移动GUI时会使其崩溃。

大家好,是的,是的,我在这里和Google上查找了一些类似的帖子,我可能想念金苹果,所以如果这个问题被真正回答了40次,那我真的很抱歉。 / p>

所以我正在使用Python,我的脚本基本上是两个循环,一个小循环,直到您按下某个键为止,您一直被“卡住”;另一个循环则包含了所有实际的好东西。

我的问题是GUI,一切正常,问题是当您与Windows交互时崩溃。我尝试查看线程,但是我没有设法使其正常工作。而且由于该脚本基本上是“ while 1:”,所以我可以理解GUI不喜欢它...

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

如果愿意,我愿意更改大多数代码,但我仍处于早期开发阶段。尤其是放下Tkinter,我“几乎”可以确定我可以使用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")

希望您能帮助我!

Lyxilion。

1 个答案:

答案 0 :(得分:1)

由于内部while循环阻止了窗口的更新,因此您可以在内部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()