python tkinter 封装成类函数

要把 tkinter 界面写成类函数放在一个模块里(easy_excel)供主程序process_compare调用。没有写过类,类下面的各个def
函数要如何串起来呢?
画布上一个processbar 展示文件读取进度,两个button start 和quit , quit,start button不显示. start button 的command
写在了主程序里面,ob.update_progress(files, command_bt)报错。
欢迎指教。

class WindowManager():#https://www.5axxw.com/questions/content/fii6kx
    
    # initializing the root
    def __init__(self):
        self.root = tkinter.Tk()
        self.root.title('Step 1 of 4') 
        self.root.geometry('300x280+600+300')
        #self.root.title(title) #,title,geometry
        #self.root.geometry(geometry)
        self.root.withdraw()

                
    # creating the progress bar and packing it to the root    
    def create_progress_bar(self):
        self.root.wm_deiconify()
        self.progress_bar = ttk.Progressbar(self.root, length = 200, mode = 'determinate', maximum = 100)
        self.progress_bar.pack(padx=10,pady=20)
    
    # function to start the mainloop
    def start_application(self):
        self.root.mainloop()
    
    # updating the progress bar from the background thread
    def update_progress(self, files, command_bt):        
        self.button_start(command_bt)
        self.start_application()
        self.progress_bar['value'] += 100/len(files)
        print('percent: {:.2%}'.format(self.progress_bar['value']/100))
        self.progress_bar.update() # 更新画面
        self.button_quit()
        
    # closing the root function
    def close(self):
        self.root.destroy()

    def button_start(self,command_bt):
        self.start = ttk.Button(self.root, text="Start", command = command_bt)
        self.start.pack(pady=10)
        
    def button_quit(self):
        self.quit = ttk.Button(self.root, text="Quit", command = self.close())
        self.quit.pack(pady=20)
        #self.root.mainloop() #启动应用程序,启动消息循环
            
ob = WindowManager()
ob.update_progress(files, command_bt)# 调用对象的函数           

下面是主程序对progressbar 和start button 的调用

    EE.WindowManager().creat_progress_bar()
    def start():
        global mergeSingleV,mergeStableV,mergeSingleM,mergeSingleMStep,mergeStableM,mergeOther        
        mergeSingleV = mergeStableV = mergeSingleM = mergeSingleMStep = mergeStableM = mergeOther = EE.create_df_type()
        # mergeSingleV = EE.create_df_type()
        # mergeStableV = EE.create_df_type()
        # mergeSingleM = EE.create_df_type()
        # mergeSingleMStep = EE.create_df_type()
        # mergeStableM = EE.create_df_type()
        # mergeOther = EE.create_df_type()
        mergeSingleV = mergeStableV = mergeSingleM = mergeSingleMStep = mergeStableM = mergeOther = EE.create_df_type()
        for i in range(0,len(files)):
            print(files[i])  
            tracepath = os.path.join(tracedir,files[i])                           
            data = pd.read_csv(tracepath,index_col= False) 
            data['Filename'] = files[i]
            data = data.merge(configP[['Par','AnalyClass']],on ='Par', how='left') 
            data = data.merge(configS[['Par','Step','AnalyClass']], on=['Par','Step'],how='left') # AnalyClass_x, AnalyClass_y 
            data['Class'] = np.where(data['AnalyClass_x'].notnull(), data['AnalyClass_x'], data['AnalyClass_y'])# Bug here AnalyClass_y failed                    
            singleV = data[data['Class']==1].drop(['AnalyClass_x','AnalyClass_y'],axis=1) # only ok4 file# na 'Parameter'
            stableV = data[data['Class']==2].drop(['AnalyClass_x','AnalyClass_y'],axis=1) # dplyr::filter(data, class==2)[,1:5]
            singleM = data[data['Class']==3].drop(['AnalyClass_x','AnalyClass_y'],axis=1) #,'Parameter'
            singleMStep = data[data['Class']==4].drop(['AnalyClass_x','AnalyClass_y'],axis=1) #?? 空
            stableM = data[data['Class']==5].drop(['AnalyClass_x','AnalyClass_y'],axis=1) 
            other = data[data['Class'].isnull()].drop(['AnalyClass_x','AnalyClass_y'],axis=1) 
            
            mergeSingleV = mergeSingleV.append(singleV) # <- rbind(mergeSingleV,singleV)# cbind() 把矩阵横向合并成一个大矩阵(列方式),而rbind()是纵向合并(行方式)。
            mergeSingleV[['Step','Interval']] = mergeSingleV[['Step','Interval']].astype(float)
            mergeStableV = mergeStableV.append(stableV) #keep data width and data columns the same for appending
            mergeSingleM = mergeSingleM.append(singleM) #Filename 
            mergeSingleMStep = mergeSingleMStep.append(singleMStep)# Last column:Filename not Class
            mergeStableM = mergeStableM.append(stableM) 
            mergeOther = mergeOther.append(other)            
            EE.WindowManager().update_progress(files, start)
            # pb['value'] += 100/len(files)
            # print('percent: {:.2%}'.format(pb['value']/100))
            # pb.update() 
    EE.WindowManager().button_start(start)   
    EE.WindowManager().button_quit()

image.png

阅读 1.9k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏