【问题标题】:tkinter progress bar mismatch "for i" iterationstkinter 进度条不匹配“for i”迭代
【发布时间】:2020-07-10 15:55:37
【问题描述】:

我有一些代码在其中迭代数据帧Tickers。 它包含 10 行(基于 Nr_of_Tickers 使其动态化),我想添加一个进度条,每次迭代都会发生变化。 每次迭代后我都有要更改的标签编号,但我的绿色进度条没有跟随迭代进度。

我已尝试更改这部分代码以同步绿色进度条,但我还没有真正掌握使其工作的逻辑。

for i in range(idx):
  sleep(0.1)

任何想法如何调整进度条?

从图中可以看出,绿色的进度条就是这样。

复制粘贴和复制问题的示例代码:

import pandas as pd
import numpy as np
from datetime import datetime

# https://www.askpython.com/python-modules/tkinter/tkinter-spinbox-and-progressbar-widgets # source code 1
# https://stackoverflow.com/questions/47896881/progressbar-with-percentage-label # source code 2
# importing tkinter module 
from tkinter import *
from tkinter import ttk
from tkinter.ttk import Progressbar
import time
import tkinter as tk


from tkinter.ttk import Progressbar, Style, Button
from time import sleep

## Progress bar settings ###########################
# Create the master object
master = tk.Tk()
 
style = ttk.Style(master)
# add label in the layout
style.layout('text.Horizontal.TProgressbar', 
             [('Horizontal.Progressbar.trough',
               {'children': [('Horizontal.Progressbar.pbar',
                              {'side': 'left', 'sticky': 'ns'})],
                'sticky': 'nswe'}), 
              ('Horizontal.Progressbar.label', {'sticky': ''})])
# set initial text
style.configure('text.Horizontal.TProgressbar', text='0 %')

# create progressbar
variable = tk.DoubleVar(master)

# Create a progressbar widget
progress_bar = ttk.Progressbar(master, style='text.Horizontal.TProgressbar', orient="horizontal",
                              mode="determinate", maximum=100, value=0, variable=variable, length = 150)

# change the text of the progressbar, 
# the trailing spaces are here to properly center the text
style.configure('text.Horizontal.TProgressbar', text="0 %      ")

def increment():
    for i in range(idx):
      sleep(0.1)
      progress_bar.step()  # increment progressbar 
      style.configure('text.Horizontal.TProgressbar', text='{:02.1f} %      '.format(((idx+1)/Nr_of_Tickers)*100))
      master.update()
      


# And a label for it
label_1 = tk.Label(master, text='Progress Bar: ')
  
# Use the grid manager
label_1.grid(row=0, column=0)
progress_bar.grid(row=0, column=1)


####################################################



Tickers = pd.DataFrame({'Stock ID': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'colors': ['red', 'white', 'blue', 'yellow', 'pink', 'grey', 'green', 'cyan', 'magenta', 'orange']})
# print(Tickers)

Colores = []
Nr_of_Tickers = Tickers.shape[0]
print(Nr_of_Tickers)

# Progress Bar
while progress_bar['value'] < Nr_of_Tickers:
## starting the loop
    for idx, row in Tickers.iterrows():
        increment()

        Stock_ID_row = Tickers.loc[idx, 'Stock ID']
        Colores = Tickers.loc[idx, 'colors']
        print(Stock_ID_row)
        print(Colores)       

print('Done')

【问题讨论】:

  • 在我的脑海中,pbar 只是粘性“ns”。您是否尝试过将其更改为其他内容?
  • 是的,我有。我尝试了不同的组合:“n”, “s”, “w”, or “e”。不幸的是,他们都没有解决我的进度条问题。但我至少解决了我的百分比标签:)。但是进度条仍然不同步。

标签: python python-3.x pandas tkinter progress-bar


【解决方案1】:

所以我遇到的问题是我没有声明每次迭代我的栏应该增加多少步。 progress_bar.step() -> progress_bar.step(x) -> progress_bar.step(increment_step)

所以我声明了一个变量increment_step,其中每次迭代时该条应增加的步数,然后我将该变量用于我的进度条。请注意,变量应该是一个常数(条形应该在迭代总数中增加相同的数量 (Nr_of_Tickers))。

完整的工作示例代码如下:

# https://www.askpython.com/python-modules/tkinter/tkinter-spinbox-and-progressbar-widgets
# https://stackoverflow.com/questions/47896881/progressbar-with-percentage-label
# importing tkinter module 
from tkinter import *
from tkinter import ttk
from tkinter.ttk import Progressbar
import time
import tkinter as tk


from tkinter.ttk import Progressbar, Style, Button
from time import sleep

## Progress bar settings ###########################
# Create the master object
master = tk.Tk()
 
style = ttk.Style(master)
# add label in the layout
style.layout('text.Horizontal.TProgressbar', 
             [('Horizontal.Progressbar.trough',
               {'children': [('Horizontal.Progressbar.pbar',
                              {'side': 'left', 'sticky': 'ns'})],
                'sticky': 'nswe'}), 
              ('Horizontal.Progressbar.label', {'sticky': ''})])
# set initial text
style.configure('text.Horizontal.TProgressbar', text='0 %')

# create progressbar
variable = tk.DoubleVar(master)

# Create a progressbar widget
progress_bar = ttk.Progressbar(master, style='text.Horizontal.TProgressbar', orient="horizontal",
                              mode="determinate", maximum=100, value=0, variable=variable, length = 150)

# change the text of the progressbar, 
# the trailing spaces are here to properly center the text
style.configure('text.Horizontal.TProgressbar', text="0 %      ")

increment_step = []

def increment():
    for i in range(1):
      increment_step = (((1)/Nr_of_Tickers)*100)
      print(increment_step)
      sleep(0.5)
      progress_bar.step(increment_step)  # increment progressbar 
      style.configure('text.Horizontal.TProgressbar', text='{:02.1f} %      '.format(((idx+1)/Nr_of_Tickers)*100))
      #progress_bar.update
      master.update()


# And a label for it
label_1 = tk.Label(master, text='Progress Bar: ')
  
# Use the grid manager
label_1.grid(row=0, column=0)
progress_bar.grid(row=0, column=1)


####################################################



Tickers = pd.DataFrame({'Stock ID': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'colors': ['red', 'white', 'blue', 'yellow', 'pink', 'grey', 'green', 'cyan', 'magenta', 'orange']})
# print(Tickers)

Colores = []
Nr_of_Tickers = Tickers.shape[0]
print(Nr_of_Tickers)

for idx, row in Tickers.iterrows():
    increment()

    Stock_ID_row = Tickers.loc[idx, 'Stock ID']
    Colores = Tickers.loc[idx, 'colors']
    print(Stock_ID_row)
    print(Colores)       

print('Done')

【讨论】:

    猜你喜欢
    • 2016-01-18
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 2019-06-04
    • 2023-04-11
    • 2018-11-02
    相关资源
    最近更新 更多