python oop learning communication between function inside a class

pascal z barpasc at yahoo.com
Thu Sep 17 18:23:59 EDT 2020


Hello,
I would like to know how possible it is to call a member function from a class and pass it a variable

Example:

class Application(tk.Frame):
    """docstring for ."""

    def __init__(self, parent):
        super(Application, self).__init__(parent)
        self.parent = parent
        parent.title('Courses selections')
        #parent.geometry("700x350")
        parent.geometry('500x320+0+0') #Width x Height
        # Create widgets/grid
        self.create_widgets()
        self.selected_item = 0

    def create_widgets(self):
        ### FIRST NAME LABEL + ENTRY
        self.firstName_txt = tk.StringVar()
        self.firstName_lbl = tk.Label(self.parent, text='First Name', font=('bold'))
        self.firstName_lbl.place(x=20,y=10)
        self.firstName_entry = tk.Entry(self.parent, textvariable=self.firstName_txt)
        self.firstName_entry.place(x=120,y=10)

  		...


    def prereq(self):
        self.boo = 1

        if self.firstName_txt.get() == "":
            msg.showwarning("Missing information", "First name info missing")
            boo = 0
        elif self.lastName_txt.get() == "":
            msg.showwarning("Missing information", "Last name info missing")
            boo = 0
        elif self.age_txt.get() == "":
            msg.showwarning("Missing information", "Age info missing")
            boo = 0
        elif self.rBtnGender.get() == 0:
            msg.showwarning("Missing information", "Gender info missing")
            boo = 0

        if self.boo == 1:
            self.fname = self.firstName_txt.get()
            self.lname = self.lastName_txt.get()
            self.age = int(self.age_txt.get())

            self.selectedCourse = self.coursesLBX.get(self.coursesLBX.curselection())

            if self.age < 21:
                msg.showwarning("Invalid Age", "Invalid Age, you are not eligible")
                return
            elif self.age >= 21:
                pass

            ### SELECTED COURSE
            if self.selectedCourse == "Quality Management (Adv.)":
                self.prereq = "The prereq for this course is Quality Management (Int)."
                self.flag = 1
            elif self.selectedCourse == "Financial Management (Adv.)":
                self.prereq = "The prereq for this course is Financial Management (Bas)."
                self.flag= 1
            elif self.selectedCourse == "Project Management (Adv.)":
                self.prereq = "The prereq for this course is Project Management (Int)."
                self.flag = 0
            else:
                self.prereq = "The prereq for this course is Project Management (Bas)."
                self.flag = 0

            ### PART TIME
            if self.chkBtnPTime.get() == 1 and self.flag == 0:
                self.str2 = "\nThis course is not available part time."
            elif self.chkBtnPTime.get() == 1 and self.flag == 1:
                self.str2 = "\nThis course is available part time."
            else:
                self.str2 = ""

            self.result = self.prereq + self.str2
            msg.showinfo('Form info', self.result)


    def save2db(self):
        try:
            db.insert(self.fname, self.lname, self.age)
            msg.showinfo('DB action', "Selection inserted into db")
        except:
            msg.showinfo("Form submission failed", "Plz check ur input")

##################################

all script available on github 
https://github.com/barpasc/python_tuto_400_oop

In function save2db, I would like to know if there is any alternative to using try/except. The alternative I'm thinking is something like 

def save2db(self,boo):
   if boo == 1:
      do something
   else:
      do something like return to previous step...

Is this possible?






More information about the Python-list mailing list