Python-Tk面向对象的编程继承属性

时间:2017-04-23 10:56:08

标签: python object inheritance button tkinter

所以我的程序是一个测验,其中有一个主类,它是一个整体反应途径,在其他类中有副反应途径。如何继承像QUESTIONSTARTER这样的页面中定义的按钮和标签,并且可以显示在第二阶段的第二阶段。这对我来说是个大问题,希望你能帮助我。这些是两个类的示例:(我无法添加一个问题启动器的方法)所以我希望问题启动器中的按钮和标签继承到第2阶段,我可以将按钮网格化为第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)

#Sets最大阶段数     def SetNonCompulsoryReagents(self):

    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)

0 个答案:

没有答案