0

This is a Chemistry Quiz and i created the QUESTIONSTARTER class as an object. I am trying to access the widgets from that class and place it in the STAGE1 Class so i can grid them but on the window nothing shows up on those frames so I don't know what to do. (I also want to access tk widgets from button data too which is another class but i cant show it due to space)

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 the Main class and tk frame constructor in each class from stack overflow
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
Ammar
  • 1
  • 2
  • I recommend removing as much code as possible from the example. Create two pages with a single widget each to make your example easier to understand. For more help see [How to create a Minimal, Complete. and Verifiable Example](http://stackoverflow.com/help/mcve) – Bryan Oakley Apr 27 '17 at 14:47

1 Answers1

0

You can't do what you want. Widgets live in a tree-like structure with parent/child relationships, and a widget cannot be moved up the tree and then down a different branch. Also any given widget cannot be displayed in more than one place at a time so if you put it in page A, in order to put it in page B you must remove it from page A.

The only solution is to duplicate the widget. If they are to show the same data, you can have them both share the same variable (ie: the textvariable attribute). When two or more widgets share the same textvariable, changes made in one widget will instantly be made in the other.

For more information about accessing widgets in other pages, see How to access variables from different classes in tkinter python 3

For more information about the architecture of the code you started with, see https://stackoverflow.com/a/7557028/7432

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • So i have to make the tk butons and labels again in that class and just grid them. For example with Attempts Displayed? Im little confused cause its a big part of my program. @bryan-oakley – Ammar Apr 27 '17 at 13:45
  • Look at what i have replied with if you didnt get it @BryanOakley – Ammar Apr 27 '17 at 13:48
  • @Ammar: you can't have one widget in two places. You'll have to create two widgets. – Bryan Oakley Apr 27 '17 at 14:48