I'm relatively new to Tkinter and I need help.
I have created a Connect database window when a button(login) is clicked from the parent window. The new window is the login.py(file). But it's not opening
connect database.py below;
from tkinter import *
from tkinter import ttk
from PIL import Image,ImageTk
import os
import pickle
import mysql.connector as sql
from tkinter import messagebox
import login
def login1():
host = host_entry.get()
port = port_entry.get()
username = username_entry.get()
password = password_entry.get()
database="cars"
spec=sql.connect(host=host,user=username,password=password,port=port)
if spec.is_connected():
messagebox.showinfo("Connected","Database connected Sucessfully")
else:
messagebox.showerror("Exists", "Database is already connected")
spec.close()
root = Tk()
root.geometry("1067x600")
root.configure(background="black")
root.resizable(False, False)
root.title("School Diaries")
#background image
bg = ImageTk.PhotoImage(file="files\Sublime Light1.jpg")
lbl_bg = Label(root,image=bg)
lbl_bg.place(x=0,y=0,relwidth=1,relheight=1)
#Labels
host_label = Label(root, text="Host Name ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 12, "bold"))
host_label.place(x=675, y=115)
host_entry = Entry(root, highlightthickness=0, relief=FLAT, bg="white", fg="#6b6a69",font=("yu gothic ui semibold", 12))
#host_entry.insert(0, "localhost")
host_entry.place(x=687, y=139, width=145)
port_label = Label(root, text="Port ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
port_label.place(x=675, y=190)
port_entry = Entry(root, highlightthickness=0, relief=FLAT, bg="white", fg="#6b6a69",font=("yu gothic ui semibold", 12))
#port_entry.insert(0, "3307")
port_entry.place(x=690, y=213, width=145)
username_label = Label(root, text="Username ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
username_label.place(x=675, y=265)
username_entry = Entry(root, highlightthickness=0, relief=FLAT, bg="white", fg="#6b6a69",font=("yu gothic ui semibold", 12))
#username_entry.insert(0, "root")
username_entry.place(x=687, y=287, width=145)
password_label = Label(root, text="Password ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
password_label.place(x=675, y=338)
password_entry = Entry(root, highlightthickness=0, relief=FLAT, bg="white", fg="#6b6a69",font=("yu gothic ui semibold", 12))
#password_entry.insert(0, "root")
password_entry.place(x=687, y=361, width=145)
#buttons
submit = ImageTk.PhotoImage(file='Pics\submit.png')
submit_button = Button(root, image=submit,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white",borderwidth=0, background="white", cursor="hand2",command=login1)
submit_button.place(x=655, y=440)
login = ImageTk.PhotoImage(file='Pics\login.png')
login_button = Button(root, image=login,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white",borderwidth=0, background="white", cursor="hand2",commmand=login)
login_button.place(x=785, y=442)
root.mainloop()
image of connect database
login.py file below ;
from tkinter import *
from tkinter import ttk
from PIL import Image,ImageTk
import os
import pickle
import mysql.connector as sql
from tkinter import messagebox
import user_inter
def login():
host = user_inter.host_entry.get()
port = user_inter.port_entry.get()
username = username_entry.get()
password = password_entry.get()
spec=sql.connect(host=host,user=username,password=password,port=port)
if spec.is_connected():
messagebox.showinfo("Connected","Database connected Sucessfully")
else:
messagebox.showerror("Exists", "Failed")
mycur=spec.cursor()
mycur.execute()
root = Tk()
root.geometry("1067x600")
root.configure(background="black")
root.resizable(False, False)
root.title("School Diaries")
#background
bg = ImageTk.PhotoImage(file="files\login.jpg")
lbl_bg = Label(root,image=bg)
lbl_bg.place(x=0,y=0,relwidth=1,relheight=1)
#Labels
login_lbl = Label(root, text="Login", bg="white", fg="#4f4e4d",font=("San Francisco", 45))
login_lbl.place(x=757,y=120)
username_label = Label(root, text="Username ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
username_label.place(x=675, y=190)
username_entry = Entry(root, highlightthickness=0, relief=FLAT, bg="white", fg="#6b6a69",font=("yu gothic ui semibold", 12))
#username_entry.insert(0, "root")
username_entry.place(x=695, y=215, width=190)
password_label = Label(root, text="Password ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
password_label.place(x=675, y=280)
password_entry = Entry(root, highlightthickness=0, relief=FLAT, bg="white", fg="#6b6a69",font=("yu gothic ui semibold", 12))
#password_entry.insert(0, "root")
password_entry.place(x=692, y=305, width=190)
login = ImageTk.PhotoImage(file='Pics\login.png')
login_button = Button(root, image=login,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white",borderwidth=0, background="white", cursor="hand2",command=login)
login_button.place(x=770, y=390)
root.mainloop()
image of login page
PS ; connect database.py is mentioned in the code as user_intern.py
0
now my doubt is ;
I have created a login.py page using tkinter and I have created a connect database.py page also, so my doubt is I have a button called login in connect database window, now the how to give argument such as if I click the login button the screen shd change to my login.py screen .....hope u understand my doubt
when I click login the login.py shd execute in the parent window itself
Thanks,
any edits in code to get my output is welcomed
when i click login in connect database the login windows shd open in parent window itself
as mentioned by #ablmmcu in comment, tkinter only has 1 root, and it should be in main program
the other point is, i suggest using tkinter.TopLevel() and use it as a window to your database when you press login button and it succeed at logging in.
more info about TopLevel:
Toplevel widgets work as windows that are directly managed by the window manager. They do not necessarily have a parent widget on top of them.
Your application can use any number of top-level windows.
source: https://www.tutorialspoint.com/python/tk_toplevel.htm
on a side note : you place TopLevel windows inside the root.
Related
I'm new to python and decided to fiddle about with a bit of GIU and was wondering how can I switch between frames in a different file dynamically? I've tried adding command=Navigator.change_to_page(self, createMenuFrame) to a button but for some reason, it just combines the frames into one. Is the way I'm trying to do this doable or better to stick with a single main.py file?
I've structured my project in the following way:
main.py:
from tkinter import *
import views.viewMaker as viewMaker
root = Tk()
root.geometry("1200x700")
root.resizable(0, 0)
# Main Frame
mainFrame = Frame(root, bg="gray", width=1200, height=700)
mainFrame.pack()
mainFrame.pack_propagate(0)
viewMaker.ViewMaker.define_views(mainFrame)
root.mainloop()
views/viewMaker.py:
from tkinter import *
import commands.navigator as Navigator
class ViewMaker:
def define_views(self):
mainMenuFrame = Frame(self, bg="white", width=300, height=700) #Define Main Menu
createMenuFrame = Frame(self, bg="white", width=300, height=700) #Define Creator Menu
ViewMaker.populate_main_menu(mainMenuFrame, createMenuFrame) #Populate Main Menu
ViewMaker.populate_create_menu(createMenuFrame) #Populate Create Menu
mainMenuFrame.pack()
mainMenuFrame.pack_propagate(0)
def populate_main_menu(self, createMenuFrame): #Define Main Menu Design
mainMenuTitle = Label(self, text="Title", bg="white", font=("Georgia", 16, "bold"))
mainMenuTitle.pack(pady=25)
generateButton = Button(self, text="Generate New", width=300, font=("Georgia", 12, "bold"))
generateButton.pack(pady=5, side=TOP)
createButton = Button(self, text="Create New", width=300, font=("Georgia", 12, "bold"), command=Navigator.change_to_page(self, createMenuFrame))
createButton.pack(pady=5, side=TOP)
quitButton = Button(self, text="Quit", width=300, font=("Georgia", 12, "bold"), command=self.master.master.destroy)
quitButton.pack(pady=25, side=BOTTOM)
def populate_create_menu(self): #Define Create Menu Design
createMenuTitle = Label(self, text="Create New", bg="white", font=("Georgia", 16, "bold"))
createMenuTitle.pack(pady=25)
commands/navigator.py:
from tkinter import *
def change_to_page(self, newPage):
newPage.pack()
newPage.pack_propagate()
self.pack_forget()
Note that command=Navigator.change_to_page(self, createMenuFrame) will execute Navigattor.change_to_page() immediately which shows the createMenuFrame.
Change it to command=lambda: Navigator.change_to_page(self, createMenuFrame) instead.
I'm new to tkinter gui framework of python
I have a doubt in treeview widget of the tkinter gui
my doubt is how to resize the treeview widget so that i can place the two treeview widget aside photos attached below and i doing a project on mysql database so for the frontend i used tkinter gui
Screenshot of output
from tkinter import *
from tkinter import ttk
from PIL import Image,ImageTk
import os
import pickle
import mysql.connector as sql
from tkinter import messagebox
def click_home():
"""set to default home tab where details like no. of students, employees, department shows """
home_frame = Frame(root)
home_frame.place(x=120, y=105, height=440, width=910)
home_dashboard_fram_r=Image.open('files\home_new.png')
home_dashboard_fram_r=home_dashboard_fram_r.resize((910,440),Image.ANTIALIAS)
home_dashboard_frame = ImageTk.PhotoImage(home_dashboard_fram_r)
home_panel = Label(home_frame, image=home_dashboard_frame, bg="white")
home_panel.image= home_dashboard_frame
home_panel.pack(fill='both', expand='yes')
def click_manage():
""" opens new frame from where one can go to manage students, employees, departments, course, section
and batch"""
manage_frame = Frame(root, bg="white")
manage_frame.place(x=120, y=105, height=440, width=910)
manage_dashboard_frame_r = Image.open('files\\manage_frame1.png')
manage_dashboard_frame_r=manage_dashboard_frame_r.resize((910,440),Image.ANTIALIAS)
manage_dashboard_frame = ImageTk.PhotoImage(manage_dashboard_frame_r)
manage_panel = Label(manage_frame, image=manage_dashboard_frame, bg="white")
manage_panel.image = manage_dashboard_frame
manage_panel.pack(fill='both', expand='yes')
student_r=Image.open('Pics\\student.png')
student_r=student_r.resize((100,100),Image.ANTIALIAS)
student = ImageTk.PhotoImage(student_r)
student_button = Button(manage_frame, image=student, relief=FLAT, borderwidth=0,activebackground="white", bg="white", cursor="hand2")
student_button.image = student
student_button.place(x=120, y=115)
employee_r=Image.open('Pics\\employee.png')
employee_r=employee_r.resize((100,100),Image.ANTIALIAS)
employee = ImageTk.PhotoImage(employee_r)
employee_button = Button(manage_frame, image=employee, relief=FLAT, borderwidth=0,activebackground="white", bg="white", cursor="hand2")
employee_button.image =employee
employee_button.place(x=395, y=115)
department_r=Image.open('Pics\\department.png')
department_r=department_r.resize((100,100),Image.ANTIALIAS)
department = ImageTk.PhotoImage(department_r)
department_button = Button(manage_frame, image=department, relief=FLAT, borderwidth=0,activebackground="white", bg="white", cursor="hand2")
department_button.image = department
department_button.place(x=672, y=115)
course_r=Image.open('Pics\\course.png')
course_r=course_r.resize((100,100),Image.ANTIALIAS)
course = ImageTk.PhotoImage(course_r)
course_button = Button(manage_frame, image=course, relief=FLAT, borderwidth=0,activebackground="white", bg="white", cursor="hand2")
course_button.image = course
course_button.place(x=121, y=300)
section_r=Image.open('Pics\\section.png')
section_r=section_r.resize((100,100),Image.ANTIALIAS)
section = ImageTk.PhotoImage(section_r)
section_button = Button(manage_frame, image=section, relief=FLAT, borderwidth=0,activebackground="white", bg="white", cursor="hand2")
section_button.image = section
section_button.place(x=396, y=300)
batch_r=Image.open('Pics\\batch.png')
batch_r=batch_r.resize((100,100),Image.ANTIALIAS)
batch = ImageTk.PhotoImage(batch_r)
batch_button = Button(manage_frame, image=batch, relief=FLAT, borderwidth=0,activebackground="white", bg="white", cursor="hand2")
batch_button.image = batch
batch_button.place(x=673, y=300)
def click_view():
""" Displays partial data into tree view of students, employees, departments, courses when clicked view tab
on interface """
view_frame = Frame(root, bg="white")
view_frame.place(x=120, y=105, height=440, width=910)
view_dashboard_frame_r=Image.open('Pics\\view_frame.png')
view_dashboard_frame_r=view_dashboard_frame_r.resize((910,440),Image.ANTIALIAS)
view_dashboard_frame = ImageTk.PhotoImage(view_dashboard_frame_r)
view_panel = Label(view_frame, image=view_dashboard_frame, bg="white")
view_panel.image=view_dashboard_frame
view_panel.pack(fill='both', expand='yes')
department_view_label = Label(view_frame, text="View Department Information ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
department_view_label.place(x=770, y=290)
course_view_label = Label(view_frame, text="View Course Information ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
course_view_label.place(x=170, y=290)
# ========================================================================
# ============================Displaying Student Information==============
# ========================================================================
student_view_label = Label(view_frame, text="View Students Information ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
student_view_label.place(x=170, y=6)
view_student_frame = Frame(view_frame, bg="white")
view_student_frame.place(x=10, y=40, height=250, width=575)
style = ttk.Style()
style.configure("Treeview.Heading", font=('yu gothic ui', 10, "bold"), foreground="red")
style.configure("Treeview", font=('yu gothic ui', 9, "bold"), foreground="#f29b0f")
scroll_y = Scrollbar(view_student_frame, orient=VERTICAL)
scroll_x = Scrollbar(view_student_frame, orient=HORIZONTAL)
view_student_tree = ttk.Treeview(view_student_frame,columns=("STUDENT ID", "STUDENT NAME", "COURSE ENROLLED", "PHONE NO."),xscrollcommand=scroll_x.set, yscrollcommand=scroll_y.set,height=2)
scroll_x.pack(side=BOTTOM, fill=X)
scroll_y.pack(side=RIGHT, fill=Y)
scroll_x.config(command=view_student_tree.xview)
scroll_y.config(command=view_student_tree.yview)
# ==========================TreeView Heading====================
view_student_tree.heading("STUDENT ID", text="STUDENT ID")
view_student_tree.heading("STUDENT NAME", text="STUDENT NAME")
view_student_tree.heading("COURSE ENROLLED", text="COURSE ENROLLED")
view_student_tree.heading("PHONE NO.", text="PHONE NO.")
view_student_tree["show"] = "headings"
# ==========================TreeView Column====================
view_student_tree.column("STUDENT ID", width=50)
view_student_tree.column("STUDENT NAME", width=50)
view_student_tree.column("COURSE ENROLLED", width=50)
view_student_tree.column("PHONE NO.", width=50)
view_student_tree.pack(fill=BOTH, expand=1)
# ========================================================================
# =========================Displaying instructor Information==============
# ========================================================================
view_employee_frame = Frame(view_frame, bg="white")
view_employee_frame.place(x=595, y=40, height=250, width=575)
employee_view_label = Label(view_frame, text="View Employees Information ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
employee_view_label.place(x=600, y=5)
scroll_y_e = Scrollbar(view_employee_frame, orient=VERTICAL)
scroll_x_e = Scrollbar(view_employee_frame, orient=HORIZONTAL)
view_employee_tree = ttk.Treeview(view_employee_frame,columns=("EMPLOYEE ID", "EMPLOYEE NAME", "DEPARTMENT", "EMPLOYEE TYPE"),xscrollcommand=scroll_x_e.set, yscrollcommand=scroll_y_e.set)
scroll_x_e.pack(side=BOTTOM, fill=X)
scroll_y_e.pack(side=RIGHT, fill=Y)
scroll_x_e.config(command=view_employee_tree.xview)
scroll_y_e.config(command=view_employee_tree.yview)
# ==========================TreeView Heading====================
view_employee_tree.heading("EMPLOYEE ID", text="EMPLOYEE ID")
view_employee_tree.heading("EMPLOYEE NAME", text="EMPLOYEE NAME")
view_employee_tree.heading("DEPARTMENT", text="DEPARTMENT")
view_employee_tree.heading("EMPLOYEE TYPE", text="EMPLOYEE TYPE")
view_employee_tree["show"] = "headings"
# ==========================TreeView Column====================
view_employee_tree.column("EMPLOYEE ID", width=50)
view_employee_tree.column("EMPLOYEE NAME", width=150)
view_employee_tree.column("DEPARTMENT", width=100)
view_employee_tree.column("EMPLOYEE TYPE", width=100)
view_employee_tree.pack(fill=BOTH, expand=1)
root = Tk()
root.geometry("1067x600")
root.title("Dashboard")
root.resizable(False, False)
#root.iconbitmap('images\\logo.ico')
bg = ImageTk.PhotoImage(file="files\Dashboard1.jpg")
lbl_bg = Label(root,image=bg)
lbl_bg.place(x=0,y=0,relwidth=1,relheight=1)
home_r=Image.open('Pics\\home.png')
home_r=home_r.resize((50, 50), Image.ANTIALIAS)
home = ImageTk.PhotoImage(home_r)
home_button = Button(root, image=home,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white", borderwidth=0, background="white", cursor="hand2",command=click_home)
home_button.place(x=40, y=113)
manage_r=Image.open('Pics\\manage.png')
manage_r=manage_r.resize((50, 50), Image.ANTIALIAS)
manage = ImageTk.PhotoImage(manage_r)
manage_button = Button(root, image=manage,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white", borderwidth=0, background="white", cursor="hand2",command=click_manage)
manage_button.place(x=38, y=205)
view_r=Image.open('Pics\\view.png')
view_r=view_r.resize((50, 50), Image.ANTIALIAS)
view = ImageTk.PhotoImage(view_r)
view_button = Button(root, image=view,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white", borderwidth=0, background="white", cursor="hand2",command=click_view)
view_button.place(x=36, y=299)
setting_r = Image.open('Pics\\setting.png')
setting_r=setting_r.resize((50, 50), Image.ANTIALIAS)
setting = ImageTk.PhotoImage(setting_r)
setting_button = Button(root, image=setting,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white", borderwidth=0, background="white", cursor="hand2")
setting_button.place(x=38, y=395)
exit_1_r=Image.open('Pics\\exit_button.png')
exit_1_r=exit_1_r.resize((50, 50), Image.ANTIALIAS)
exit_1 = ImageTk.PhotoImage(exit_1_r)
exit_button = Button(root, image=exit_1,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white", borderwidth=0, background="white", cursor="hand2")
exit_button.place(x=37, y=490)
logout = ImageTk.PhotoImage(file='Pics\logout.png')
logout_button = Button(root, image=logout,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white", borderwidth=0, background="white", cursor="hand2")
logout_button.place(x=940, y=56)
root.mainloop()
Which arugument i should use to change the size of treeview widget ..........
use expand= True, fill = y or x to resize it.
I have made two separate GUI screens, one as login.py and user_intern.py and in user_intern.py I have a button called login_button, so when I click login the login.py which contains GUI for login screen should execute and the user_intern.py should close automatically and I didn't used class (OOP) here, I have written code fully with functions only so hereby I attached the codes and screenshots of screens.
User_intern.py
user_intern.py_pic
from tkinter import *
from tkinter import ttk
from PIL import Image,ImageTk
import os
import pickle
import mysql.connector as sql
from tkinter import messagebox
def login1():
host = host_entry.get()
port = port_entry.get()
username = username_entry.get()
password = password_entry.get()
database="cars"
spec=sql.connect(host=host,user=username,password=password,port=port)
if spec.is_connected():
messagebox.showinfo("Connected","Database connected Sucessfully")
else:
messagebox.showerror("Exists", "Database is already connected")
spec.close()
root = Tk()
root.geometry("1067x600")
root.configure(background="black")
root.resizable(False, False)
root.title("School Diaries")
#background image
bg = ImageTk.PhotoImage(file="files\Sublime Light1.jpg")
lbl_bg = Label(root,image=bg)
lbl_bg.place(x=0,y=0,relwidth=1,relheight=1)
#Labels
host_label = Label(root, text="Host Name ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 12, "bold"))
host_label.place(x=675, y=115)
host_entry = Entry(root, highlightthickness=0, relief=FLAT, bg="white", fg="#6b6a69",font=("yu gothic ui semibold", 12))
#host_entry.insert(0, "localhost")
host_entry.place(x=687, y=139, width=145)
port_label = Label(root, text="Port ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
port_label.place(x=675, y=190)
port_entry = Entry(root, highlightthickness=0, relief=FLAT, bg="white", fg="#6b6a69",font=("yu gothic ui semibold", 12))
#port_entry.insert(0, "3307")
port_entry.place(x=690, y=213, width=145)
username_label = Label(root, text="Username ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
username_label.place(x=675, y=265)
username_entry = Entry(root, highlightthickness=0, relief=FLAT, bg="white", fg="#6b6a69",font=("yu gothic ui semibold", 12))
#username_entry.insert(0, "root")
username_entry.place(x=687, y=287, width=145)
password_label = Label(root, text="Password ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
password_label.place(x=675, y=338)
password_entry = Entry(root, highlightthickness=0, relief=FLAT, bg="white", fg="#6b6a69",font=("yu gothic ui semibold", 12))
#password_entry.insert(0, "root")
password_entry.place(x=687, y=361, width=145)
#buttons
submit = ImageTk.PhotoImage(file='Pics\submit.png')
submit_button = Button(root, image=submit,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white",borderwidth=0, background="white", cursor="hand2",command=login1)
submit_button.place(x=655, y=440)
login = ImageTk.PhotoImage(file='Pics\login.png')
login_button = Button(root, image=login,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white",borderwidth=0, background="white", cursor="hand2")
login_button.place(x=785, y=442)
root.mainloop()
login.py
login.py_pic
from tkinter import *
from tkinter import ttk
from PIL import Image,ImageTk
import os
import pickle
import mysql.connector as sql
from tkinter import messagebox
def login():
#host = user_inter.host_entry.get()
#port = user_inter.port_entry.get()
username = username_entry.get()
password = password_entry.get()
spec=sql.connect(host=host,user=username,password=password,port=port)
if spec.is_connected():
messagebox.showinfo("Connected","Database connected Sucessfully")
else:
messagebox.showerror("Exists", "Failed")
mycur=spec.cursor()
mycur.execute()
root = Tk()
root.geometry("1067x600")
root.configure(background="black")
root.resizable(False, False)
root.title("School Diaries")
#background
bg = ImageTk.PhotoImage(file="files\login.jpg")
lbl_bg = Label(root,image=bg)
lbl_bg.place(x=0,y=0,relwidth=1,relheight=1)
#Labels
login_lbl = Label(root, text="Login", bg="white", fg="#4f4e4d",font=("San Francisco", 45))
login_lbl.place(x=757,y=120)
username_label = Label(root, text="Username ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
username_label.place(x=675, y=190)
username_entry = Entry(root, highlightthickness=0, relief=FLAT, bg="white", fg="#6b6a69",font=("yu gothic ui semibold", 12))
#username_entry.insert(0, "root")
username_entry.place(x=695, y=215, width=190)
password_label = Label(root, text="Password ", bg="white", fg="#4f4e4d",font=("yu gothic ui", 13, "bold"))
password_label.place(x=675, y=280)
password_entry = Entry(root, highlightthickness=0, relief=FLAT, bg="white", fg="#6b6a69",font=("yu gothic ui semibold", 12))
#password_entry.insert(0, "root")
password_entry.place(x=692, y=305, width=190)
login = ImageTk.PhotoImage(file='Pics\login.png')
login_button = Button(root, image=login,font=("yu gothic ui", 13, "bold"), relief=FLAT, activebackground="white",borderwidth=0, background="white", cursor="hand2")
login_button.place(x=770, y=390)
root.mainloop()
You can do any edits in code to get the output.
Im trying to close my login wondow and open my register window once I click the sign up button but it doesn't destroy the login window
Here is my login code
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import PIL
from PIL import ImageTk
from PIL import Image
import register
import pymysql
class Login:
def __init__(self,root):
self.root = root
self.root.title("Scheduling Management System")
self.root.geometry("1350x768+0+0")
self.root.resizable(False, False)
self.txt_user = StringVar()
self.txt_pass = StringVar()
self.bg = ImageTk.PhotoImage(file="Images/bgimage.jpg")
bg = Label(self.root, image=self.bg).place(x=0, y=0, relwidth=1, relheight=1)
framelogin = Frame(self.root, bg="white")
framelogin.place(x=450, y=100, height=500, width=700)
title = Label(framelogin, text="Login Here", font=("Arial", 30, "bold"), fg="orange", bg="white").place(x=90,
y=30)
nexttitle = Label(framelogin, text="Scheduling Staff System", font=("Times New Roman", 18, "bold"), fg="orange",
bg="white").place(x=90, y=100)
userlabel = Label(framelogin, text="Username", font=("Arial", 15, "bold"), fg="gray", bg="white").place(x=90,
y=140)
self.txt_user = Entry(framelogin, textvariable=self.txt_user, font=("times new roman", 15), bg="lightgray")
self.txt_user.place(x=90, y=170, width=350, height=35)
passlabel = Label(framelogin, text="Password", font=("Arial", 15, "bold"), fg="gray", bg="white").place(x=90,
y=210)
self.txt_pass = Entry(framelogin, textvariable=self.txt_pass, font=("times new roman", 15), show="*",
bg="lightgray")
self.txt_pass.place(x=90, y=240, width=350, height=35)
forget = Button(framelogin, text="Forgot Password", bg="white", fg="orange", font=("trebuchet ms", 12)).place(
x=90, y=305)
reglabel = Label(framelogin, text="Don't Have an Account?", font=("trebuchet ms", 12, "bold"), fg="orange",
bg="white").place(x=320, y=310)
registerbutton = Button(framelogin, text="Sign Up", command=self.register, bg="white", fg="orange",
font=("trebuchet ms", 12)).place(x=510, y=305)
loginbutton = Button(framelogin, text="Login", command=self.login, fg="white", bg="orange",
font=("sans serif ms", 20)).place(x=90, y=350, width="100", height="40")
def login(self):
if self.txt_user.get() == "" or self.txt_pass.get() == "":
messagebox.showerror("Error", "Please fill up all fields!")
def register(self):
register.RegisterForm()
if __name__ == "__main__":
root = Tk()
obj = Login(root)
root.mainloop()
Here is my register code
from tkinter import *
from tkinter import ttk, messagebox
import PIL
import pymysql
from PIL import ImageTk
from PIL import Image
import login
class Register:
def __init__(self, root):
self.root = root
...
btn = Button(frame1,image=self.btn, bd=0, command = self.registerdata,cursor = "hand2").place(x=50, y = 420)
def registerdata(self):
if self.text_fname.get()=="" or self.text_lname.get()=="" or self.text_contact.get()=="" or self.text_email.get()=="" or self.cmbquestion.get()=="Select" or self.text_pwd.get()=="" or self.text_cfmpwd.get()=="":
messagebox.showerror("Error","All fields are required!",parent=self.root)
elif self.text_pwd.get()!=self.text_cfmpwd.get():
messagebox.showerror("Error","Passwords must be the same!",parent=self.root)
else:
try:
con=pymysql.connect(host="localhost",user="root",password="",database="employee")
cur=con.cursor()
cur.execute("select * from employeelist where email=%s", self.text_email.get())
row=cur.fetchone()
print(row)
if row!=None:
messagebox.showerror("Error","User Already Exists. Please Register With a New Email",parent=self.root)
else:
cur.execute("insert into employeelist (fname,lname,contact,email,question,answer,password) values(%s,%s,%s,%s,%s,%s,%s)",
(self.text_fname.get(),self.text_lname.get(),self.text_contact.get(),self.text_email.get(),self.cmbquestion.get(),self.text_answer.get(),self.text_pwd.get()))
con.commit() #do changes to database
con.close()
messagebox.showinfo("Success","Registration Successful",parent=self.root)
except Exception as ex:
messagebox.showerror("Error",f"Error due to: {str(ex)}",parent=self.root)
l = login
l.Login(root)
def RegisterForm():
win = Toplevel()
obj = Register(win)
if __name__ == "__main__":
root = Tk()
obj = Register(root)
root.mainloop()
Any ideas on how to do so as currently I'm only able to see both windows instead of destroying login window and opening register window?
Firstly add self.root.destroy() to the register method in the login file.
This will destroy the login Tk instance.
To avoid the extra window when the register window opens, change win = Toplevel() in RegisterForm to win = Tk(). Toplevel needs a Tk window so it made an extra window, so we need to create a Tk instance instead.
I have been on this for some time now, have tried so many methods I could find online but non seems to solve my problem. My Toplevel() window displays nothing. This is supposed to be part of my program at a certain point. Had to copy the code out and summarize it this way yet it doesn't work. I think i am missing something
from tkinter import *
# from tkinter import ttk
# import sqlite3
# conn = sqlite3.connect('shengen.db')
# c = conn.cursor()
# q = c.execute(
# "SELECT email FROM users WHERE email=('ichibuokem#gmail.com');")
# conn.commit()
# for i in q:
# print(list(i))
def portal_start():
portal = Toplevel(root)
portal.title("Visa Application Form")
portal.geometry("600x600")
portal.iconbitmap("...\Visa-Application-Management-System\icon.ico")
Label(portal, text="", bg="grey", height="2",
width="900", font=("Calibri", 14)).pack(pady=10)
spc = Label(portal, text="")
spc.pack()
portal_notebook = ttk.Notebook(portal)
portal_notebook.pack()
page1 = Frame(portal_notebook, width=600, height=600)
page2 = Frame(portal_notebook, width=600, height=600)
page3 = Frame(portal_notebook, width=600, height=600)
page4 = Frame(portal_notebook, width=600, height=600)
summary = Frame(portal_notebook, width=600, height=600)
page1.pack(fill="both", expand=1)
page2.pack(fill="both", expand=1)
page3.pack(fill="both", expand=1)
page4.pack(fill="both", expand=1)
summary.pack(fill="both", expand=1)
portal_notebook.add(page1, text="Basic Details")
portal_notebook.add(page2, text="Sponsorship")
portal_notebook.add(page3, text="Medicals")
portal_notebook.add(page4, text="References")
portal_notebook.add(summary, text="Summary")
# ============================================Portal End===================================================
# =============================================Instantiation window=======================================
def window():
global root
root = Tk()
root.geometry("900x700")
root.title("Welcome Page")
root.iconbitmap("..\Visa-Application-Management-System\icon.ico")
Label(root, text="Welcome To Python Visa Application Portal! \n\nTo check your visa application status, file a new application or update your application, \nLogin or Create an account.",
fg="white", bg="grey", height="6", width="900", font=("Calibri", 14)).pack()
Label(root, text="").pack()
Label(root, text="").pack()
Label(root, text="").pack()
Label(root, text="").pack()
Label(root, text="").pack()
Button(root, text="Login", width=20, font=(
"bold", 14)).pack()
Label(root, text="").pack()
Button(root, text="Create Account", width=20,
font=("bold", 14)).pack()
Label(root, text="").pack()
Label(root, text="").pack()
Label(root, text="Copyright 2020. All Rights Reserved \nWith Luv From Group 3",
font=("Calibri", 8)).pack()
Button(root, text="Test Window", width=20,
font=("bold", 14), command=portal_start).pack()
Label(root, text="").pack()
root.mainloop()
window()
add this to the top your your code
from tkinter import ttk
It should work perfectly then