PythonTk跨对象编程继承属性

2024-09-30 22:17:35 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我的计划是一个测验,其中有一个主要的类,是一个整体的反应途径与其他类的子反应途径。我如何继承像QUESTIONSTARTER一样在一个页面中定义的按钮和标签,并且可以在另一个类STAGE 2上显示。这对我来说是个大问题,希望你能帮助我。这是两个类的例子:(我不能在question starter中再添加一个方法)所以我希望question starter中的按钮和标签被继承到stage 2中,在stage 2中我可以将按钮网格化到stage 2中

class SETOFQUESTIONS(QUESTIONSTARTER):

def __init__ (self):
    self.NumberOfStages = NumberOfStages
    self.StartingReactant = StartingReactant

def SetStages(self, NumberOfStages):
    #If the quiz type is default and customised set number of stages to a minimum of 3 stages and a max of 5 stages for aliphatic reaction
    #for aromatic -set min stages 2 and max stages 5

    if "Default" == True:
        self.NumberOfStages = 5
        print ("Number Of Stages = ",NumberOfStages)
        return NumberOfStages

    else:
        self.NumberOfStages = CustomisedNumberOfStages

        print ("Number Of Stages = ",CustomisedNumberOfStages)

#设置最大级数 def SETNONCIMPLORYREAGENTS(自身):

    SetCompulsoryReagentOptionList(self)
    ReagentsList = []


    while ReagentsList == True:
        for count in range (len(ReagentsList)):
            try:
                sql = ("""SELECT ReagentName FROM NonCompulsoryReagentDatabase \
                        ORDER BY RAND() \
                        LIMIT 1""")
                conn.execute(sql)
                NonCompulsoryReagents = conn.fetchone()
            except:
                conn.rollback()

            if NonCompulsoryReagents[0] != ReagentsList[count]:
                ReagentsList.append(NonCompulsoryReagents[0])
                ReagentsList = False

            elif NonCompulsoryReagents[1] != ReagentsList[count]:
                ReagentsList.append(NonCompulsoryReagents[1])
                ReagentsList = False

            elif NonCompulsoryReagents[2] != ReagentsList[count]:
                ReagentsList.append(NonCompulsoryReagents[2])
                ReagentsList = False

            else:
                ReagentsList = True

        for Reagent in ReagentsList:
            RandomReagent = random.choice(ReagentsList)
            ReagentOption1 = tk.Button(bottomFrame, text = RandomReagent, command=lambda: self.AllocatePoints(1))
            ReagentOption2 = tk.Button(bottomFrame, text = RandomReagent, command=lambda: self.AllocatePoints(2))
            ReagentOption3 = tk.Button(bottomFrame, text = RandomReagent, command=lambda: self.AllocatePoints(3))
            ReagentOption4 = tk.Button(bottomFrame, text = RandomReagent, command=lambda: self.AllocatePoints(4))





def SetNonCompulsoryConditions(self):
    SetCompulsoryConditionOptionList(self)
    sql = ("""SELECT ConditionName FROM NonCompulsoryConditionsDatabase \
                ORDER BY RAND ()  \
                LIMIT 1""")
    try:
            conn.execute(sql)
            NonCompulsoryConditions = conn.fetchone()
    except:
        conn.rollback()

        while ConditionsList == True:
            for count in range (len(ConditionsList)):
                sql = ("""SELECT ConditionName FROM NonCompulsoryConditionsDatabase \
                        ORDER BY RAND() \
                        LIMIT 1""")
                conn.execute(sql)
                NonCompulsoryReagents = conn.fetchone()

                if NonCompulsoryConditions[0] != ConditionsList[count]:
                    ConditionsList.append(NonCompulsoryReagents[0])
                    ConditionsList = False

                elif NonCompulsoryConditions[1] != ConditionsList[count]:
                    ConditionsList.append(NonCompulsoryConditions[1])
                    ConditionsList = False

                elif NonCompulsoryConditions[2] != ConditionsList[count]:
                    ConditionsList.append(NonCompulsoryConditions[2])
                    ConditionsList = False

                else:
                    ConditionsList = True

            for Condition in ConditionsList:
                RandomCondition = random.choice(ConditionsList)
                ConditionOption1 = tk.Button(bottomFrame, text = RandomCondition, command=lambda: self.AllocatePoints(5))
                ConditionOption2 = tk.Button(bottomFrame, text = RandomCondition, command=lambda: self.AllocatePoints(6))
                ConditionOption3 = tk.Button(bottomFrame, text = RandomCondition, command=lambda: self.AllocatePoints(7))
                ConditionOption4 = tk.Button(bottomFrame, text = RandomCondition, command=lambda: self.AllocatePoints(8))



    #call at random the specific products from product database
    #depending on class run
def ButtonPressed(self,ReagentsList):
    ReagentsList[0] = CorrectReagent
    self.CorrectReagent.configure(bg = 'green')
    IncorrectReagentsList = []
    IncorrectReagentsList.append(ReagentsList[1])
    IncorrectReagentsList.append(ReagentsList[2])
    IncorrectReagentsList.append(ReagentsList[3])

    for Reagents in IncorrectReagentsList:
        tk.Button[Reagents].configure(bg = "red")

    ConditionsList[0] = CorrectCondition
    self.CorrectCondition.configure(bg = "green")
    IncorrectConditionsList = []
    IncorrectConditionsList.append(ReagentsList[1])
    IncorrectConditionsList.append(ReagentsList[2])
    IncorrectConditionsList.append(ReagentsList[3])

    for Reagents in IncorrectReagentsList:
        tk.Button[Reagents].configure(bg = "red")

class STAGE2(SETOFQUESTIONS):
 def __init__(self,parent, controller):
      tk.Frame.__init__(self, parent)
      QuestionFrame.tkraise()
      controller.show_frame(QuestionFrame)

      Question.pack(side = "top")
      Question.grid()


      ReagentOption1.grid(column = 0,row = 0)

      ReagentOption2.grid(column = 0,row = 1)

      ReagentOption3.grid(column = 0,row = 2)

      ReagentOption4.grid(column = 0,row = 3)


      ConditionOption1.grid(column = 0,row = 0)

      ConditionOption2.grid(column = 0,row = 1)

      ConditionOption3.grid(column = 0,row = 2)

      ConditionOption4.grid(column = 0,row = 3)

      Continue = tk.Button(StageFrame, text = "Continue", command=lambda: controller.show_frame(Stage2))

      Continue.grid(column = 6)

      PointsLabel.grid(row=0,column=6)
      AttemptsDisplayed.grid(row=1,column=7)

Tags: lambdatextselfforcountcolumnbuttoncommand