tkinter 进度条不匹配 "for i" 次迭代
tkinter progress bar mismatch "for i" iterations
我有一些代码可以迭代数据帧 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
# # 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')
所以我遇到的问题是我没有声明每次迭代我的栏应该增加多少步。 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
#
# 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')
我有一些代码可以迭代数据帧 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
# # 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')
所以我遇到的问题是我没有声明每次迭代我的栏应该增加多少步。 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
#
# 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')