【问题标题】:How to have live update of printed text in the window?如何在窗口中实时更新打印文本?
【发布时间】:2019-08-07 09:29:10
【问题描述】:

我正在使用 Pysimplegui 在执行事件后在 for 循环中输出一些文本。但是,当 for 循环结束时,我只能一次看到所有文本。当 for 循环运行时,我看不到它。 gui 显示“没有响应”(但它正在运行)。知道如何解决这个问题吗?

这似乎发生在我有一个 for 循环并且在 for 循环中运行的事件需要时间来执行时。下面是我的代码。

import PySimpleGUI as sg
import time

def excecutetest(command):
        for i in range(5):
            print (command + str(i))
            time.sleep(2)

layout = [      
    [sg.Text('Information:', size=(40, 1))],      
    [sg.Output(size=(88, 20))],      
    [sg.Text('Input:', size=(15, 1)), sg.InputText(focus=True), sg.Button('Run', bind_return_key=True)],
    [sg.Button('EXIT')]      
        ] 

window = sg.Window('testing', layout)      

# ---===--- Loop taking in user input and using it to call scripts --- #      

while True:      
  (event, value) = window.Read()      
  if event == 'EXIT'  or event is None:      
      break # exit button clicked      
  if event == 'Run':      
      excecutetest(value[0])
window.Close()

我希望窗口能够实时输出每个 for 循环。 谢谢。

【问题讨论】:

    标签: python-3.x pysimplegui


    【解决方案1】:

    如果您希望在下次调用 Window.Read() 之前显示某些内容,则需要添加对 Window.Refresh() 的调用。它在文档和一些演示程序中进行了讨论。

    我知道这是测试代码,但不建议将 sleeps 放入事件循环中。

    【讨论】:

      猜你喜欢
      • 2015-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      • 1970-01-01
      • 2010-11-15
      相关资源
      最近更新 更多