Python Gui与主线程的通信
我想与gui线程通信,我如何才能做到这一点。Python Gui与主线程的通信,python,python-3.x,Python,Python 3.x,我想与gui线程通信,我如何才能做到这一点。 因为我正在处理rpg,需要实时更新gui中的文本。我建议您在ui上实现一个类接口,允许您执行您想要的操作: 大概是这样的: class gui(threading.Thread, ): def __init__(self): threading.Thread.__init__(self) def run(self): app = QApplication([]) label = QLa
因为我正在处理rpg,需要实时更新gui中的文本。我建议您在ui上实现一个类接口,允许您执行您想要的操作: 大概是这样的:
class gui(threading.Thread, ):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
app = QApplication([])
label = QLabel('Hello World!')
l1 = QLabel('Moin servus')
label.show()
l1.show()
while play:
if edit:
textedit(l1)
False
app.exec_()
if __name__ == "__main__":
t1 = gui()
t1.start()
while play:
textname = ("Willkommen beim legendären RPG!")
edit = True
wait()
textname = ("Kurze Regeln man kann mit ja [j] oder nein [n] antworten")
edit = True
wait()
textname = ("Viel Spaß Abenteurer")
edit = True
wait()
textname = ("Bist du bereit?")
edit = True
然后,您可以重写代码,告诉UI您希望发生什么:
class TextUI(threading.Thread):
# __init__ and run as before
def clear_screen(self):
"""Clear the text display area"""
pass
def writeln(self, *items):
"""Write items to next display line, then move down."""
pass
def write(self, *items):
"""Write items, leave cursor at end"""
pass
def cursor(self, state: CursorState):
"""Set state of cursor: on, blinking, off"""
pass
您是否打算使用
edit
变量进行通信?如果是这样,尾随的False
可能应该是edit=False
。请注意,如果要从函数/方法内部修改edit
,则必须声明global edit
。这与问题无关。
def game(tui: TextUI):
"""Play RPG repeatedly until player quits"""
playing = True
say = tui.writeln
while playing:
tui.clear_screen()
say("Willkommen beim legendären RPG!")
say("Kurze Regeln man kann mit ja [j] oder nein [n] antworten")
say("Viel Spaß Abenteurer")
say("Bist du bereit?")