在我发布我的代码之前,这不是全部,但我觉得与我的问题有关。当用户点击一个按钮时运行第一个类,因此显示类内容(框架)。我帧的处理程序是:来自另一个班级的一个班级的呼叫功能
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”,该功能正在运行,但只是不对我的第二帧做任何事情。它只是空白。定位元素的代码(所以它们显示)是为了在运行开始后运行...
这是否与我称之为的方式?
非常感谢您的帮助。
“FrameTwo”需要做什么_exactly_“ReviseTen”中有什么?如果它完全一样,它为什么存在?为什么不直接使用'ReviseTen'两次? –