2017-03-18 182 views
0

在我發佈我的代碼之前,這不是全部,但我覺得與我的問題有關。當用戶點擊一個按鈕時運行第一個類,因此顯示類內容(框架)。我幀的處理程序是:來自另一個班級的一個班級的呼叫功能

class Begin(tk.Tk): 
    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 
     # Creating the initial frame 
     container = tk.Frame(self) 
     container.pack(side="top", fill="both", expand=True) 
     container.grid_rowconfigure(0, weight=1) 
     container.grid_columnconfigure(0, weight=1) 

     self.frames = {} 

     for F in (LoginScreen, RegisterWindow, RevisionTopics, dataRep, reviseTen, FrameTwo): 
      frame = F(container, self) 
      self.frames[F] = frame 
      frame.grid(row=0, column=0, sticky="nsew") 
      page_name = LoginScreen.__name__ 
      self.frames[page_name] = frame 

     self.show_frame(LoginScreen) # Shows the page currently being interacted 

現在,這是具有重要的作用,「開始」,這是我需要在我的第二幀運行框架。

第一幀:

class reviseTen(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     self.startButton = tk.Button(self, text="Click here to start revision session", command = self.start) 
     self.optionOne = tk.Button(self, text="Option One") 
     self.optionTwo = tk.Button(self, text="Option Two") 
     self.optionThree = tk.Button(self, text="Option Three") 
     self.optionFour = tk.Button(self, text="Option Four") 
     self.proceedButton = tk.Button(self, text="Proceed to next question", command=lambda: controller.show_frame(FrameTwo)) 
     self.question = tk.Label(self, text="What is the definition of: ") 
     self.startButton.grid(row=0, column=0) 

    def start(self): #This is what I wanna use in my second frame 
     firstTime = True 
     while firstTime: 
      self.startButton.destroy() 
      firstTime = False 
     words = self.makeDict() 

     initialListOne = ['Integer', 'Natural number', 'Rational numbers', 'Irrational numbers', 'Real numbers', 'Ordinal numbers', 'Binary', 'Hexadecimal'] 
     listOne = [] 
     for i in initialListOne: 
      listOne.append(words[i]) 

     initialListTwo = ['Denary to Hex', 'Binary to Hex', 'ASCII', 'Unicode', 'Overflow error', 'Twos complement', 'Bitmapped graphics', 'Resolution'] 
     listTwo = [] 
     for i in initialListTwo: 
      listTwo.append(words[i]) 

     initialListThree = [ 'Bit Colour Depth', 'Metadata', 'Sample resolution', 'Sample Rate', 'Audio file size', 'Nyquist Theorem', 'MIDI', 'Lossy Compression'] 
     listThree = [] 
     for i in initialListThree: 
      listThree.append(words[i]) 

     initialListFour = ['Lossless Compression', 'Run Length Encoding', 'Dictionary compression', 'Encryption', 'Encryption steps', 'Caesar cipher', 
         'Brute force attack', 'Frequency analysis', 'Vernam cipher', 'One-Time Pad'] 
     listFour = [] 
     for i in initialListFour: 
      listFour.append(words[i]) 

     listOfKeys = [] # Holds the keywords 
     listOfValues = [] # Holds the definitions 

     for key in words: 
      listOfKeys.append(key) 
      listOfValues.append(words[key]) 

     keywordPosition = random.randint(1, len(listOfKeys)-1) 
     QKeyword = listOfKeys[keywordPosition] 
     QDef = listOfValues[keywordPosition] 


     self.question.grid(row=0, column=0) 
     self.optionOne.grid(row=1, column=0) 
     self.optionTwo.grid(row=2, column=0) 
     self.optionThree.grid(row=3, column=0) 
     self.optionFour.grid(row=4, column=0) 
     self.proceedButton.grid(row=5, column=0) 

     self.question.config(text=("What is the definition of: "+ QKeyword)) 

     randomOne = random.randint(0, len(listOne)) 
     randomTwo = random.randint(0, len(listTwo)) 
     randomThree = random.randint(0, len(listThree)) 
     randomFour = random.randint(0, len(listFour)) 

     selectButton = random.randint(1,4) 
     if selectButton == 1: 
      self.optionOne.config(text=QDef) 
      self.optionTwo.config(text=listOfValues[randomTwo]) 
      self.optionThree.config(text=listOfValues[randomThree]) 
      self.optionFour.config(text=listOfValues[randomFour]) 
     elif selectButton == 2: 
      self.optionOne.config(text=listOfValues[randomOne]) 
      self.optionTwo.config(text=QDef) 
      self.optionThree.config(text=listOfValues[randomThree]) 
      self.optionFour.config(text=listOfValues[randomFour]) 
     elif selectButton == 3: 
      self.optionOne.config(text=listOfValues[randomOne]) 
      self.optionTwo.config(text=listOfValues[randomTwo]) 
      self.optionThree.config(text=QDef) 
      self.optionFour.config(text=listOfValues[randomFour]) 
     elif selectButton == 4: 
      self.optionOne.config(text=listOfValues[randomOne]) 
      self.optionTwo.config(text=listOfValues[randomTwo]) 
      self.optionThree.config(text=listOfValues[randomThree]) 
      self.optionFour.config(text=QDef) 

    def makeDict(self): 
     dict = {} 
     con = sql.connect("dataRep.db") 
     cur = con.cursor() 
     for column in cur.execute("SELECT keyword, definition FROM words"): 
      variable = column[0] 
      variable2 = column[1] 
      dict[variable] = variable2 
     return dict 

第二幀:

class FrameTwo(tk.Frame): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     self.controller = controller 
     self.optionOne = tk.Button(self, text="Option One") 
     self.optionTwo = tk.Button(self, text="Option Two") 
     self.optionThree = tk.Button(self, text="Option Three") 
     self.optionFour = tk.Button(self, text="Option Four") 
     self.question = tk.Label(self, text="What is the definition of: ") 

    # TRIED THIS - screen stays blank (but start method has code that makes the widgets appear 
     self.start(controller) 

    def start(self, controller): 
     self.reviseTen = reviseTen(self, controller) 

我需要開始完成完全相同的功能,因爲它沒有框架「reviseTen」,該功能正在運行,但只是不對我的第二幀做任何事情。它只是空白。定位元素的代碼(所以它們顯示)是爲了在運行開始後運行...

這是否與我稱之爲的方式?

非常感謝您的幫助。

+0

「FrameTwo」需要做什麼_exactly_「ReviseTen」中有什麼?如果它完全一樣,它爲什麼存在?爲什麼不直接使用'ReviseTen'兩次? –

回答

0

繼承reviseTen在你的類,而不是tk.Frame,並調用與super功能:

class FrameTwo(reviseTen): 
    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     self.controller = controller 
     self.optionOne = tk.Button(self, text="Option One") 
     self.optionTwo = tk.Button(self, text="Option Two") 
     self.optionThree = tk.Button(self, text="Option Three") 
     self.optionFour = tk.Button(self, text="Option Four") 
     self.question = tk.Label(self, text="What is the definition of: ") 
     super(FrameTwo, self).start(controller) 

欲瞭解更多信息的super,檢查出一些問題的答案,以this question

+0

哪個括號?你的意思是括號?如果是這樣,哪個括號? – Julien

+1

很高興我能幫忙:) – Julien

相關問題