gpt4 book ai didi

python - ttk.Progressbar 被复制

转载 作者:太空宇宙 更新时间:2023-11-04 05:10:19 25 4
gpt4 key购买 nike

我在显示包含 tkinter.StringVar 变量的小部件时遇到了一个特殊问题。

无论我是否使用框架来定位变量,都会发生这种情况。

简而言之,我有三个非变量标签、一个变量标签、一个进度条和两个垂直排列的按钮(按钮并排放置(虽然无关紧要,但为了完整性而提供)。

当以编程方式更改变量时,连接新字符 ('\n) 和更多文本时,变量会显示额外的行,但会显示重复的进度条和按钮:

screen shots of progressbar at various stages

(单独的图像 here )。

有趣的注意事项:如果未添加 '\n',则不会发生这种情况。

import os
import sys
import time
import tkinter as tk
import tkinter.ttk as ttk

class rtGetPaths(tk.Frame):
"""
"""
def __init__(self, root):
tk.Frame.__init__(self, root)

self.root = root

# Get the system separator
self.path_sep = os.pathsep
# Find the root on the drive
self.root_id = os.path.abspath(os.sep)
self.data = [(os.path.join('C:', 'Program Files', 'App_1.exe')),
(os.path.join('C:', 'Program Files', 'App_2.exe')),
(os.path.join('C:', 'Program Files', 'App_3.exe')),
(os.path.join('C:', 'Program Files', 'App_4.exe'))
]

self.locns = []

self.root.title('Get Paths')

self.gpw_l1 = tk.Label(self.root, text='Searching for apps')
self.gpw_l2 = tk.Label(self.root, text='This may take some time')
self.gpw_l3 = tk.Label(self.root, text='Please be patient')

self.gpw_found_l_svar = tk.StringVar()
self.gpw_found_l_svar.set('')
self.gpw_found_l = tk.Label(self.root, textvariable=self.gpw_found_l_svar)

self.gpw_pb_ivar = tk.IntVar()
self.gpw_pb_ivar.set(0)
self.gpw_pb_length = 350
self.gpw_pb_max = 5
self.gpw_pb = ttk.Progressbar(self.root,
mode='determinate',
maximum=self.gpw_pb_max,
length=self.gpw_pb_length,
variable=self.gpw_pb_ivar)

self.gpw_exit_b = tk.Button(self.root,
text='Exit',
command=sys.exit)
self.gpw_continue_b = tk.Button(self.root,
text='Continue',
command=self.gpw_action_continue_b)

row = 0
self.gpw_l1.grid(row=row, columnspan=2)
row += 1
self.gpw_l2.grid(row=row, columnspan=2)
row += 1
self.gpw_l3.grid(row=row, columnspan=2)
row += 1
self.gpw_found_l.grid(row=row, columnspan=2)
row += 1
self.gpw_pb.grid(row=row, columnspan=2)
row += 1
self.gpw_exit_b.grid(row=row, column=0, sticky='ew')
self.gpw_continue_b.grid(row=row, column=1, sticky='ew')

self.gpw_found_l.grid_remove()

self.root.geometry('+100+200')

def gpw_action_continue_b(self):
"""

:return:
"""
for file in self.data:
lookfor = ''
if 'App_1.exe' in file:
lookfor = file
elif 'App_2.exe' in file:
lookfor = file
elif 'App_3.exe' in file:
lookfor = file
elif 'App_4.exe' in file:
lookfor = file

if lookfor != '':
self.locns.append(lookfor)

label = self.gpw_found_l_svar.get()
if 0 < self.gpw_pb_ivar.get() < 5:
label = label + '\n'
label = label + os.path.join(lookfor)
self.gpw_found_l_svar.set(label)

self.gpw_pb_ivar.set(self.gpw_pb_ivar.get() + 1)
if not self.gpw_found_l.winfo_viewable():
self.gpw_found_l.grid()

self.root.update_idletasks()
time.sleep(1)

self.gpw_continue_b['state'] = 'disabled'
self.gpw_pb_ivar.set(self.gpw_pb_max)
self.root.update_idletasks()

return

#==============================================================================
# MAIN (MAIN)
#==============================================================================
def main():
""" Run the app
"""
# # Create the screen instance and name it
root = tk.Tk()
app = rtGetPaths(root)
root.mainloop()
root.quit()

#==============================================================================
# MAIN (STARTUP)
#==============================================================================
if __name__ == '__main__':
# Run the function name main()
main()

它为什么这样做,我该如何阻止它?

最佳答案

我不确定为什么会出现此问题,但您可以通过每次更改标签时更新进度条来解决此问题

代码:

def gpw_action_continue_b(self):

for file in self.data:
...
self.gpw_pb.update() # this fixes the problem
self.root.update_idletasks()
time.sleep(1)

证明有效:

enter image description here

关于python - ttk.Progressbar 被复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43161096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com