2017-04-01 37 views
2

显示包含tkinter.StringVar变量的窗口小部件时,我遇到了一个特殊问题。ttk.Progressbar被复制

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

简言之,我有三个非可变标签,一个可变标记,进度和两个按钮所有网格垂直(按钮是并排虽然(无关但出于完整性提供)。

当可变以编程方式更改,串接一个新字符('\ n)和更多的文本中,可变显示了额外的线路,但重复progressbars并显示按钮:

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() 

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

+0

究竟如何做你想要瘦gs看?随着更多的文件被发现,它应该放在哪里? – martineau

+0

Hi @martineau,进度条和按钮随着线条的添加而向下移动。我接受了Tom Fuller的回答。 – Garry

回答

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

+0

现在,这只是纯粹的巫术! 非常感谢,我花了这么长时间。我会给你买一杯饮料。 – Garry

+0

没问题:)很高兴我能帮上忙 –