从一个类访问Tk Inter小部件并在另一个类上显示

时间:2017-04-27 11:04:13

标签: python oop tkinter widget tk

这是一个化学测验,我创建了QUESTIONSTARTER类作为对象。我试图从该类访问小部件并将其放在STAGE1类中,这样我就可以对它们进行网格化,但是在窗口上没有任何东西显示在这些框架上,所以我不知道该怎么做。 (我也想从按钮数据访问tk小部件,这是另一个类,但由于空间我无法显示)

class QUESTIONSTARTER(tk.Frame):
 def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller = controller
    self.Attmepts = 3
    self.Points = 0
    self.NumberOfTimesSubmitBeenPressed = 0
    self.WordedQuestion = tk.Label(self)
    self.PointsLabel = tk.Label(self,text = self.Points)        
    self.AttemptsDisplayed = tk.Button(self, text = self.Attempts)
    self.AttemptsDisplayedText = tk.Label(self, text = "Attempts Displayed: ")

 def SetQuestionIntro(self):     
    WordedQuestion.config(text = "Many different organic compounds are synthesised by various routes but they may need specific type of reagents and conditions.")


 def SubmitClicked(self, Attempts):

    if self.Attempts == 1:
        NumberOfTimesSubmitBeenPressed += 1
    elif self.Attempts == 2:
        NumberOfTimesSubmitBeenPressed += 2
    elif self.Attempts == 3:
        NumberOfTimesSubmitBeenPressed += 2

    if NumberOfTimesSubmitBeenPressed == 3:
        controller.show_frame(F)
    else:
        print ("There was an Error")    

    #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")




 def AllocatePointsStage1(self,frames ,ButtonPressed, Attempts, Points):


      while self.Attempts == 1:
           if self.ReagentOption1.bg == 'green' or self.ConditionOption1.bg == 'green':
            self.Points += 20
            controller.show_frame(Stage2)
            self.frames.remove(Stage2)
            self.frames.insert(Stage2)

           elif self.ReagentOption2.bg == 'green'or self.ConditionOption2.bg == 'green':
            self.Points += 20
            self.show_frame(Stage2)
            self.frames.remove(Stage2)
            self.frames.insert(Stage2)

           elif self.ReagentOption3.bg == 'green' or self.ConditionOption3.bg == 'green':
            self.Points += 20
            self.show_frame(Stage2)
            self.frames.remove(Stage2)
            self.frames.insert(Stage2)

           elif self.ReagentOption4.bg == 'green' or self.ConditionOption4.bg == 'green':
            self.Points += 20
            self.show_frame(Stage2)
            self.frames.remove(Stage2)
            self.frames.insert(Stage2)

           elif self.ReagentOption1.bg == 'red' or self.ConditionOption1.bg == 'red':
            self.Points += 0
            self.Attempts += 1

           elif self.ReagentOption2.bg == 'red' or self.ConditionOption2.bg == 'red':
            self.Points += 0
            self.Attempts += 1

           elif self.ReagentOption3.bg == 'red' or self.ConditionOption3.bg == 'red':
            self.Points += 0
            self.Attempts+= 1

           elif self.ReagentOption4.bg == 'red' or self.ConditionOption4.bg == 'red':
            self.Points += 0
            self.Attempts += 1

class STAGE1(tk.Frame):
 def __init__(self,parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller = controller

    Stage1 = tk.Label(self, text = "STAGE 1")
    Stage1.grid(column=1,row=0)          

#Explain来自堆栈溢出的每个类中的Main类和tk框架构造函数             self.ButtonData = BUTTONDATA(self,tk.Frame)
            self.ButtonData.ReagentOption1.config(command = lambda:self.AllocatePointsStage(1))         self.ButtonData.ReagentOption2.config(command = lambda:self.AllocatePointsStage(1))         self.ButtonData.ReagentOption3.config(command = lambda:self.AllocatePointsStage(1))         self.ButtonData.ReagentOption4.config(command = lambda:self.AllocatePointsStage(1))

    self.ButtonData.ConditionOption1.config(command=lambda: self.AllocatePointsStage(1))
    self.ButtonData.ConditionOption2.config(command=lambda: self.AllocatePointsStage(1))
    self.ButtonData.ConditionOption3.config(command=lambda: self.AllocatePointsStage(1))
    self.ButtonData.ConditionOption4.config(command=lambda: self.AllocatePointsStage(1))

    self.ButtonData.ReagentOption1.grid(column = 2,row = 5)
    self.ButtonData.ReagentOption2.grid(column = 2,row = 6)
    self.ButtonData.ReagentOption3.grid(column = 2,row = 7)
    self.ButtonData.ReagentOption4.grid(column = 2,row = 8) 

    self.ButtonData.ConditionOption1.grid(column = 10,row = 5)
    self.ButtonData.ConditionOption2.grid(column = 10,row = 6)
    self.ButtonData.ConditionOption3.grid(column = 10,row = 7)   
    self.ButtonData.ConditionOption4.grid(column = 10,row = 8)

    self.Continue = tk.Button(self, text = "Continue", command=lambda: controller.show_frame(Stage2))
    self.Continue.grid(column = 6)

    self.QuestionStarter = QUESTIONSTARTER(self,tk.Frame)
    self.QuestionStarter.PointsLabel.grid(row=0,column=6)
    self.QuestionStarter.AttemptsDisplayed.grid(row=1,column=7)
    self.QuestionStarter.WordedQuestion.grid(row=0,column=1)
    self.QuestionStarter.AttemptsDisplayedText.grid(row=1,column=8)
    DesiredProductLabel = tk.Label(self,command=lambda: ShowDesiredProduct(DesiredProduct))
    DesiredProductLabel.grid(row=5,column=0)

 def ShowDesiredProduct(self,DesiredProduct):
    DesiredProduct = GetDesiredProduct()
    return DesiredProduct

1 个答案:

答案 0 :(得分:0)

你无法做你想做的事。窗口小部件生活在具有父/子关系的树状结构中,窗口小部件无法向上移动,然后向下移动到另一个分支。此外,任何给定的窗口小部件一次不能在多个位置显示,因此如果您将其放在页面A中,为了将其放在页面B中,您必须将其从页面A中删除。

唯一的解决方案是复制小部件。如果要显示相同的数据,则可以让它们共享相同的变量(即:textvariable属性)。当两个或多个小部件共享相同的textvariable时,在一个小部件中进行的更改将立即在另一个小部件中进行。

有关在其他页面中访问小部件的详细信息,请参阅How to access variables from different classes in tkinter python 3

有关您开始使用的代码体系结构的详细信息,请参阅https://stackoverflow.com/a/7557028/7432