I'm trying to use Tkinter to create a GUI for a school planner and I want to have users be able to log in and create an account. It might get too lengthy if I try to code the entire project in one file, so I'm trying to split it up. My main file just has:
try:
import tkinter as tk # python 3
from tkinter import font as tkfont # python 3
except ImportError:
import Tkinter as tk # python 2
import tkFont as tkfont # python 2
import TkWindow
app = TkWindow.TkWindow()
app.getRoot().mainloop()
Where my TkWindow
file is the file that runs everything. It looks like this:
from tkinter import *
import mainMenu
import CreateAccount
class TkWindow:
def __init__(self):
self.master = Tk() # Makes the window
self.master.wm_title("School Planner")
self.master.config(background="#FFFFFF")
# Master window dimensions
app_width = 1000
app_height = 500
screen_width = self.master.winfo_screenwidth() # Get screen width
screen_height = self.master.winfo_screenheight() # Get screen height
x = int((screen_width / 2) - (app_width / 2)) # X coordinate to center application
y = int((screen_height / 2) - (app_height / 2)) # Y coordinate to center application
self.dimensions = f'{app_width}x{app_height}+{x}+{y}'
self.master.geometry(self.dimensions) # Set master geometry
container = Frame(self.master, width=app_width, height=app_height, relief='raised', borderwidth=5)
# self.currentFrame = Frames.main(self.master)
self.frames = {}
for F in (mainMenu.MainMenu, CreateAccount.CreateAccount):
page_name = F.__name__
frame = F(master=container.master, controller=self)
self.frames[page_name] = frame
self.show_frame("MainMenu")
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
def getRoot(self):
return self.master
Where my mainMenu
file is:
from tkinter import *
import Fonts
import CreateAccount
class MainMenu(Frame):
def __init__(self, master, controller):
Frame.__init__(self, master)
self.root = master
self.root.title("Benjamin's School Planner")
# TODO: get an icon and find the path of it
# master.iconbitmap('HERE SHOULD GO THE FILE PATH TO THE ICON')
# Master window dimensions
app_width = 1000
app_height = 500
#screen_width = master.winfo_screenwidth() # Get screen width
#screen_height = master.winfo_screenheight() # Get screen height
#x = int((screen_width / 2) - (app_width / 2)) # X coordinate to center application
#y = int((screen_height / 2) - (app_height / 2)) # Y coordinate to center application
#master.geometry(f'{app_width}x{app_height}+{x}+{y}') # Set master geometry
# Set and display welcome message
welcome_label = Label(self.root, text='Welcome to the School Planner', font=Fonts.welcomeFont())
welcome_label.pack()
# Set and display username prompt
username_label = Label(self.root, text='Username', font=Fonts.loginFont())
username_label.place(relx=0.4, rely=0.4, anchor='center')
username_entry = Entry(self.root)
username_entry.place(relx=0.55, rely=0.4, anchor='center')
# Set and display username prompt
password_label = Label(self.root, text='Password', font=Fonts.loginFont())
password_label.place(relx=0.4, rely=0.5, anchor='center')
password_entry = Entry(self.root, show="u2022")
password_entry.place(relx=0.55, rely=0.5, anchor='center')
# Login button command:
def login():
print("Logged in Successfully")
# Login button creation:
login_button = Button(text='Login',
command=lambda: login(),
bg='Gray', fg='Black', font=Fonts.loginFont())
login_button.place(relx=0.5, rely=0.62, anchor='center')
# 'Show Password' Checkbox creation
checkboxVar = IntVar()
def showPassword():
if checkboxVar.get() == 1:
password_entry.config(show="")
password_entry.update()
elif checkboxVar.get() == 0:
password_entry.config(show="u2022")
show_password_checkbox = Checkbutton(master,
text="Show Password",
font=Fonts.showPasswordFont(),
variable=checkboxVar,
command=showPassword)
show_password_checkbox.place(relx=0.5, rely=0.7, anchor='center')
# Create Account button creation:
create_account_button = Button(text='Create Account',
command=lambda: controller.show_frame("CreateAccount"),
bg='Gray', fg='Black', font=Fonts.createAccountFont())
create_account_button.place(relx=0.5, rely=0.7, anchor='center')
And my CreateAccount
file is:
from tkinter import *
import Fonts
class CreateAccount(Frame):
def __init__(self, master, controller):
Frame.__init__(self, master)
#master.title("Create Account")
master.config(bg='blue')
# Master window dimensions
app_width = 1000
app_height = 500
#screen_width = master.winfo_screenwidth() # Get screen width
#screen_height = master.winfo_screenheight() # Get screen height
#x = int((screen_width / 2) - (app_width / 2)) # X coordinate to center application
#y = int((screen_height / 2) - (app_height / 2)) # Y coordinate to center application
#master.geometry(f'{app_width}x{app_height}+{x}+{y}') # Set master geometry
welcome_label = Label(master, text='We made it to create account', font=Fonts.welcomeFont())
welcome_label.pack()
I'm having trouble where when it runs, it's running both frames at the same time at the beginning and I'm not sure why. Whenever I press the "create account" button, it registers that a frame should switch, but the new one doesn't arise. Does anyone know why?
question from:
https://stackoverflow.com/questions/65841544/how-to-make-a-tkinter-frame-raise-from-different-files