有人可以给我看一个不在循环中使用 pysimplegui 的例子吗——也许作为我可以手动更新的定义设置

can somebody show me an example of using pysimplegui not in a loop - maybe as a definition setup that I can update manually

我正在寻找一种使用 PYSimpleGUI 进度条的方法...没有循环 我在网上找了好几天都没找到例子。

似乎每个人都用循环或定时器来做他们的例子。

我想做一些更像是我可以调用来更新的定义

我不知道要更改什么才能使其成为手动更新的项目... 我希望能够在我的脚本开头告诉它 i=0 并通过脚本定期放置更新标记 (i=i+4) 这样我就可以在脚本中的每个主要步骤完成时更新它

这是 PySimpleGUI 脚本,加上一些显示我想要做什么的行 这当前自动迭代...我不知道如何更改它

我只是想学习,无法在网上找到任何示例来做我想做的事情。

import PySimpleGUI as sg
import time
from time import sleep


import PySimpleGUI as sg

def prog():

    layout = [[sg.Text('Completed Tasks')],      
          [sg.ProgressBar(100, orientation='h', size=(50, 20), key='progressbar')],      
          [sg.Cancel()]]


    window = sg.Window('Progress').Layout(layout)      
    progress_bar = window.FindElement('progressbar')      

    for i in range(100):      
        event, values = window.Read(timeout=0)      
        progress_bar.UpdateBar(i + 4)
    time.sleep(2)
    window.Close()

prog()



time.sleep(2)
#______________________________________________________________

#I'd like to be able to do this

#i=0 at this point
prog()
#do Scripty Stuff

#Update Progress Bar Manually
#i=4 at this point

#do more scriptic writings

#Update Progress bar Manually
#i=8 at this point

#and so forth and so on until I reach 100

想通了

将所有内容保留为一行,而不是定义

这是一个可以帮助他人的例子

我刚刚在更新栏部分做了实数 但您可以使用变量 (i=0) 然后在脚本中使用 i=i+1 更新它 然后在更新栏功能中使用我作为您的号码

i=0
progress_bar.UpdateBar(i, 5) 
#i returns a value of 0

i=i+1
progress_bar.UpdateBar(i, 5) 
#i now returns a vlaue of 1

#repeat until you reach your maximum value




#this Script will create a Progress Bar
#The Progress will be Manually Updated using the format listed below
#progress_bar.UpdateBar(Value of Bar, Maximum Bar Value)
#the Window uses a .Finalize Function to make the window Persistent

#Import the PySimpleGUI Library
import PySimpleGUI as sg
#Import the Time Library for use in this script
import time

#this is for the Layout Design of the Window
layout = [[sg.Text('Custom Text')],
              [sg.ProgressBar(1, orientation='h', size=(20, 20), key='progress')],
          ]
#This Creates the Physical Window
window = sg.Window('Window Title', layout).Finalize()
progress_bar = window.FindElement('progress')

#This Updates the Window
#progress_bar.UpdateBar(Current Value to show, Maximum Value to show)
progress_bar.UpdateBar(0, 5)
#adding time.sleep(length in Seconds) has been used to Simulate adding your script in between Bar Updates
time.sleep(.5)

progress_bar.UpdateBar(1, 5)
time.sleep(.5)

progress_bar.UpdateBar(2, 5)
time.sleep(.5)

progress_bar.UpdateBar(3, 5)
time.sleep(.5)

progress_bar.UpdateBar(4, 5)
time.sleep(.5)

progress_bar.UpdateBar(5, 5)
time.sleep(.5)
#I paused for 3 seconds at the end to give you time to see it has completed before closing the window
time.sleep(3)

#This will Close The Window
window.Close()


给在 2021 年或之后找到这个答案的每个人的简短说明: window.FindElement() 已弃用,现在使用 window.find_element() :)

@soundtechscott:感谢您的回答,我今天也遇到了这个问题,您的解决方案对我很有效。