【问题标题】:can somebody show me an example of using pysimplegui not in a loop - maybe as a definition setup that I can update manually有人可以向我展示一个不在循环中使用 pysimplegui 的示例 - 也许作为我可以手动更新的定义设置
【发布时间】:2019-06-09 17:39:28
【问题描述】:

我正在寻找一种使用 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

【问题讨论】:

    标签: python progress-bar pysimplegui


    【解决方案1】:

    想通了

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

    这是一个帮助他人的例子

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

    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()
    
    
    

    【讨论】:

      【解决方案2】:

      给所有在 2021 年或以后找到此答案的人的简短说明: window.FindElement() 已弃用,如今有人使用 window.find_element() :)

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多