ttk.Progressbar被复制

时间:2017-04-01 19:29:09

标签: python tkinter

在显示包含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()

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

1 个答案:

答案 0 :(得分:1)

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

代码:

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