I just started working on a little project to help me study for a course I'm taking.
Is there anything I can do to improve code readability / bad practices?
I had to remove most of the labels and buttons (due to stack-overflow posting rules) but I kept one of everything just so you can sus out how I programmed those features.
import tkinter as tk
import pandas as pd
import datetime as datetime
import random
#Global Variables
labelH1 = ("Verdana", 20)
labelH2 = ("Verdana", 17)
labelParagraph = ("Verdana", 13)
labelButton = ("Verdana", 11)
########################################################################
class GUI:
""""""
def __init__(self, root):
"""Constructor"""
self.root = root # root is a passed Tk object
#Custom Window Height
#TODO! change to ratio
window_height = 700
window_width = 1000
#Get User Screen Info
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
#Center GUI
x_cordinate = int((screen_width/2) - (window_width/2))
y_cordinate = int((screen_height/2) - (window_height/2))
#Set the Window's Position and dimensions
root.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))
########################################################################
#LANDING PAGE
def mainMenu(self):
self.frame = tk.Frame(self.root)
self.frame.pack()
self.lastPage = "Main Menu"
#TEXT/LABELS
header = tk.Label(self.frame, text="Welcome to a quiz", font=labelH1 )
header.grid(row=0, column = 3, ipady =20)
SBJMath = tk.Label(self.frame, text="Math", font=labelH2 )
SBJMath.grid(row=1, column = 3)
#BUTTONS
#MATHS QUIZZES
BTN_BasicMath = tk.Button(self.frame, text="Primer Statistics", font=labelButton, command = lambda: self.quizInstructions("BasicMath"))
BTN_BasicMath.grid(row=2,column=2, pady=10)
#EXTRA BUTTONS
BTNQuit = tk.Button(self.frame, text="Quit", font=labelButton, command =root.destroy)
BTNQuit.grid(row=13, column=0)
########################################################################
#ABOUT QUIZ PAGE
def quizInstructions(self, quizName):
self.removethis()
self.lastPage = "Quiz About"
quizHeader = ""
quizAbout = ""
chooseQuiz = ""
if quizName == "BasicMath":
quizHeader = "Primer Statistics"
quizAbout = """big line"""
chooseQuiz = ""
self.frame = tk.Frame(self.root)
self.frame.pack()
tk.Label(self.frame, text=quizHeader, font=labelH1 ).grid(row=0, column=3, pady = 20)
tk.Label(self.frame, text=quizAbout, font=labelParagraph, wraplength=600,anchor="n" ).grid(row=1, column=3, pady = 30)
tk.Button(self.frame, text="Go Back", font=labelButton, command=self.returnToLastFrame).grid(row=2, column=3, sticky = tk.W)
tk.Button(self.frame, text="Start Quiz", font=labelButton, command=self.quiz ).grid(row=2, column=3, sticky = tk.E)
########################################################################
#QUIZ GUI
def quiz(self):
self.lastPage = "Quiz"
self.removethis()
self.frame = tk.Frame(self.root)
self.frame.pack()
self.quizLogic()
########################################################################
#QUIZ LOGIC
def quizLogic(self):
pass
def removethis(self):
self.frame.destroy()
#Go back
def returnToLastFrame(self):
self.removethis()
if self.lastPage == "Main Menu":
pass
if self.lastPage == "Quiz About":
self.mainMenu()
if self.lastPage == "Quiz":
self.quizInstructions()
else:
pass
#----------------------------------------------------------------------
if __name__ == "__main__":
root = tk.Tk()
window = GUI(root).mainMenu()
root.mainloop()