ttk.进度条被复制

2024-09-25 00:36:23 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

简而言之,我有三个非变量标签,一个变量标签,一个progressbar和两个按钮都垂直网格化(虽然按钮是并排的(不相关,但为了完整性而提供的)。在

当以编程方式更改变量时,连接新字符('\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()

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


Tags: pathselfapposrootexetkgrid
1条回答
网友
1楼 · 发布于 2024-09-25 00:36:23

我不知道为什么会出现这个问题,但是您可以通过每次更改标签时更新进度条来解决它

代码:

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

相关问题 更多 >