Development with Python and wxFormbuilder

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
finchy70
In need of some credit
In need of some credit
Posts: 2
Joined: Wed Sep 25, 2019 9:19 am

Development with Python and wxFormbuilder

Post by finchy70 » Wed Sep 25, 2019 9:52 am

I have written a few GUI application using Python and WxFormbuilder. I would like to understand how I can refactor my code to reduce code duplication.

I have a few forms with a lot of TextCtrl's. Currently I have lines of code that populate each box.
This makes for a lot of almost identical code.

Code: Select all

def populate(self, emp):

    
	#Monday
    
	emp.m_mon1.SetValue(str(self.mon1))
    
	emp.m_mon1_c.SetValue(str(self.mon1_climbing))
    
	emp.m_mon2.SetValue(str(self.mon2))
    
	emp.m_mon2_c.SetValue(str(self.mon2_climbing))
    
	emp.m_mon3.SetValue(str(self.mon3))
    
	emp.m_mon3_c.SetValue(str(self.mon3_climbing))
    
	mon1_total_value = (self.mon1 + self.mon1_climbing) if (self.mon1 + self.mon1_climbing + self.mon2 + self.mon2_climbing 
												+ self.mon3 +
  self.mon3_climbing) < 8.0 
												else (self.mon1 + self.mon1_climbing - 0.5)
                                    				   
	emp.m_mon1_tot.SetLabel(str(mon1_total_value))
    
	mon2_total_value = self.mon2 + self.mon2_climbing
    
	emp.m_mon2_tot.SetLabel(str(mon2_total_value))
    
	mon3_total_value = self.mon3 + self.mon3_climbing
    
	emp.m_mon3_tot.SetLabel(str( mon3_total_value))
    
	emp.m_mon_tot.SetValue(str(mon1_total_value + mon2_total_value + mon3_total_value))
    
	mon1_job_number = Job.find(self.mon1_job_id)

	mon2_job_number = Job.find(self.mon2_job_id)

	mon3_job_number = Job.find(self.mon3_job_id)
    
	if mon1_job_number is not None:
        
		emp.m_mon1_job.SetValue(str(mon1_job_number.job_no))
        
		emp.m_mon1_site.SetLabel(str(mon1_job_number.site + " - " + mon1_job_number.client.name))
    
	if mon2_job_number is not None:
        
		emp.m_mon2_job.SetValue(str(mon2_job_number.job_no))
        
		emp.m_mon2_site.SetLabel(str(mon2_job_number.site + " - " + mon2_job_number.client.name))
    
	if mon3_job_number is not None:
        
		emp.m_mon3_job.SetValue(str(mon3_job_number.job_no))
        
		emp.m_mon3_site.SetLabel(str(mon3_job_number.site + " - " + mon3_job_number.client.name))

    
		
	#Tuesday
    emp.m_tue1.SetValue(str(self.tue1))
    
	emp.m_tue1_c.SetValue(str(self.tue1_climbing))
    
	emp.m_tue2.SetValue(str(self.tue2))
    
	emp.m_tue2_c.SetValue(str(self.tue2_climbing))
    
	emp.m_tue3.SetValue(str(self.tue3))
    
	emp.m_tue3_c.SetValue(str(self.tue3_climbing))
    
	tue1_total_value = (self.tue1 + self.tue1_climbing) if (self.tue1 + self.tue1_climbing +
 self.tue2 + self.tue2_climbing 
											+ self.tue3 +
 self.tue3_climbing) < 8.0 
											else (self.tue1 + self.tue1_climbing - 0.5)
    						 
        emp.m_tue1_tot.SetLabel(str(tue1_total_value))

        tue2_total_value = self.tue2 + self.tue2_climbing
    
        emp.m_tue2_tot.SetLabel(str(tue2_total_value))
    
        tue3_total_value = self.tue3 + self.tue3_climbing
    
        emp.m_tue3_tot.SetLabel(str(tue3_total_value))
    
        tue1_job_number = Job.find(self.tue1_job_id)
    
        tue2_job_number = Job.find(self.tue2_job_id)
    
        tue3_job_number = Job.find(self.tue3_job_id)
    
        if tue1_job_number is not None:

        	emp.m_tue1_job.SetValue(str(tue1_job_number.job_no))

        	emp.m_tue1_site.SetLabel(str(tue1_job_number.site + " - " + tue1_job_number.client.name))

        if tue2_job_number is not None:
        
        	emp.m_tue2_job.SetValue(str(tue2_job_number.job_no))
        
        	emp.m_tue2_site.SetLabel(str(tue2_job_number.site + " - " + tue2_job_number.client.name))

        if tue3_job_number is not None:
        
        	emp.m_tue3_job.SetValue(str(tue3_job_number.job_no))
        
        	emp.m_tue3_site.SetLabel(str(tue3_job_number.site + " - " + tue3_job_number.client.name))
As you can see lots of duplication.

I have thought about including the TextCtrl identifiers in an array
box_ids = [m_mon1, m_mon2, m_mon3]

then looping through

Code: Select all

for id in box_id:
to iterate through the boxes.

I would then need to do something similar with the data.

Is there a standard pattern for this sort of problem when working with wxPython?

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3787
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Development with Python and wxFormbuilder

Post by ONEEYEMAN » Mon Sep 30, 2019 2:33 pm

Hi,
Create a generic function where you put a repeated code and call it where needed.

Thank you.

finchy70
In need of some credit
In need of some credit
Posts: 2
Joined: Wed Sep 25, 2019 9:19 am

Re: Development with Python and wxFormbuilder

Post by finchy70 » Mon Sep 30, 2019 3:13 pm

This is a function call populate but what I wanted to do was call it for each day but not sure how to pass in the correct widget ID.

Post Reply