2017-04-01 35 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

沒問題:)很高興我能幫上忙 –