移动时Python GUI崩溃

2024-04-26 11:01:38 发布

您现在位置:Python中文网/ 问答频道 /正文

太长了,读不下去了,为什么我的GUI崩溃了?你知道吗

嗨,首先,是的,我在这里和谷歌上查了一些类似的帖子,我可能错过了金苹果,所以如果这个问题真的被回答了40次,那么我真的很抱歉。你知道吗

所以我使用的是Python,我的脚本基本上是两个循环,一个小循环,你被“卡住”直到按下一个键,还有一个更大的循环,包含了所有实际的好东西。你知道吗

我的问题是GUI,一切正常,问题是当你与windows交互时它崩溃了。我试图寻找线程,但我没有设法使它工作,因为我想它。由于脚本基本上是一个“while1:”我可以理解GUI不喜欢它。。。你知道吗

GUI本身只是作为一个输出,按钮是不需要的,我不需要一个巨大的刷新率。你知道吗

如果需要的话,我愿意修改我的大部分代码,我还处于早期开发阶段。尤其是drop 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")

希望你能帮助我!你知道吗

利希利昂。你知道吗


Tags: theto代码text脚本outputstringcreate
1条回答
网友
1楼 · 发布于 2024-04-26 11:01:38

由于内部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()

相关问题 更多 >