Display different frames based on logged in value in tkinter - python-3.x

I am creating an office management application. In this, there is admin and staff. In the Log In page based on whether it is admin or staff different frames needs to be displayed.
I used an if-else statement for this purpose.I tried to compare the value of username using get() but was unable to print its value inside the function.
Following is the code I executed.
import tkinter as tk
from tkinter import ttk
import mysql.connector
class SchoolApp(tk.Tk):
BKGR_IMAGE_PATH = 'images\\bg11.png'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.geometry("1500x750")
main_frame = tk.Frame(self,width=200,height=50,highlightbackground="black",highlightthickness=1)
main_frame.pack(side='top', fill='both', expand='True')
main_frame.grid_rowconfigure(0, weight=1)
main_frame.grid_columnconfigure(0, weight=1)
self.bkgr_image = tk.PhotoImage(file=self.BKGR_IMAGE_PATH)
self.frames = {}
for F in (HomePage,LogIn,AdminPage,StaffPage):
frame = F(main_frame, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky='nsew')
self.show_frame(HomePage)
def show_frame(self,container):
frame = self.frames[container]
frame.tkraise()
class BasePage(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
label_bkgr = tk.Label(self, image=controller.bkgr_image)
label_bkgr.place(x=0,y=0) # Center label w/image.
class HomePage(BasePage):
def __init__(self, parent, controller):
super().__init__(parent, controller)
home_frame = tk.Frame(self, width=200, height=100, background="white")
home_frame.grid(row=50, column=150, padx=300, pady=100)
self.label_title = tk.Label(home_frame, text="Office Management System", font=("Helvetica", 40), bg="white")
self.label_title.grid(row=0, column=0)
staff_frame = tk.Frame(home_frame, width=100, height=50)
staff_frame.grid(row=55, column=0, padx=50, pady=50)
self.staff_Image = tk.PhotoImage(file="images\\staff1.png")
self.staff_Image_Pack = tk.Label(staff_frame, image=self.staff_Image)
self.staff_Image_Pack.grid()
self.staff_button = tk.Button(home_frame, text="Staff", width=20, font=("Helvetica", 12),command = lambda: controller.show_frame(LogIn))
self.staff_button.grid(row=60, column=0, pady=20)
admin_frame = tk.Frame(home_frame, width=100, height=50)
admin_frame.grid(row=55, column=10, padx=50, pady=50)
self.Admin_Image = tk.PhotoImage(file="images\\admin1.png")
self.Admin_Image_Pack = tk.Label(admin_frame, image=self.Admin_Image)
self.Admin_Image_Pack.grid()
self.admin_button = tk.Button(home_frame, text="Admin", width=20, font=("Helvetica", 12),command = lambda: controller.show_frame(LogIn))
self.admin_button.grid(row=60, column=10, pady=20)
class LogIn(BasePage):
def __init__(self,parent,controller):
super().__init__(parent, controller)
login_window = tk.Frame(self, width=200, height=100, background="white")
login_window.grid(row=50, column=150, padx=300, pady=100)
login_img_frame = tk.Frame(login_window, width=200, height=50, background="white")
login_img_frame.grid(row=55, column=0, padx=50, pady=50)
self.login_Image = tk.PhotoImage(file="images\\lock3.png")
self.login_Image_Pack = tk.Label(login_img_frame, image=self.login_Image)
self.login_Image_Pack.grid()
login_frame = tk.Frame(login_window, width=100, height=50, background="white", highlightbackground="black",highlightthickness=1)
login_frame.grid(row=55, column=10, padx=50, pady=50)
self.label_title = tk.Label(login_frame, text="Log In", font=("Helvetica", 40), bg="white")
self.label_title.grid(row=0, column=20, padx=10, pady=10)
self.label_username = tk.Label(login_frame, text="Username", font=("Helvetica", 20), bg="white")
self.label_username.grid(row=50, column=20, padx=10, pady=10)
self.entry_username = tk.Entry(login_frame, width=15, font=("Helvetica", 20), bd=3)
self.entry_username.grid(row=50, column=30, padx=10, pady=10)
self.label_password = tk.Label(login_frame, text="Password", font=("Helvetica", 20), bg="white")
self.label_password.grid(row=60, column=20, padx=10, pady=10)
self.entry_password = tk.Entry(login_frame, width=15, font=("Helvetica", 20), bd=3)
self.entry_password.grid(row=60, column=30, padx=10, pady=10)
#print(self.entry_username.get())
if(self.entry_username.get()=="admin"):
self.login_button = tk.Button(login_frame, text="Log In", command=lambda:[self.submit(),controller.show_frame(AdminPage)],font=("Helvetica", 20), bg="white")
self.login_button.grid(row=70, column=25, padx=10, pady=10)
else:
self.login_button = tk.Button(login_frame, text="Log In", command=lambda:[self.submit(),controller.show_frame(StaffPage)],font=("Helvetica", 20), bg="white")
self.login_button.grid(row=70, column=25, padx=10, pady=10)
def submit(self):
self.u_name = self.entry_username.get()
self.p_word = self.entry_password.get()
employee = mysql.connector.connect(host="localhost", user="root", password="", database="edatabase")
cursor_variable = employee.cursor()
cursor_variable.execute("INSERT INTO login VALUES ('" + self.u_name + "','" + self.p_word + "')")
employee.commit()
employee.close()
class AdminPage(BasePage):
def __init__(self,parent,controller):
super().__init__(parent, controller)
admin_window = tk.Frame(self, width=200, height=100, background="white")
admin_window.grid(row=50, column=150, padx=300, pady=100)
self.label_title_admin = tk.Label(admin_window, text="Welcome Admin", font=("Helvetica", 40), bg="white")
self.label_title_admin.grid(row=0, column=0, padx=10, pady=10, sticky='W')
admin_add = tk.Frame(admin_window, width=100, height=50)
admin_add.grid(row=55, column=0, padx=50, pady=50)
self.admin_add_Image = tk.PhotoImage(file="images\\add3.png")
self.admin_add_Image_Pack = tk.Label(admin_add, image=self.admin_add_Image)
self.admin_add_Image_Pack.grid()
self.add_staff_button = tk.Button(admin_window, text="Add Staff", width=15, font=("Helvetica", 12))
self.add_staff_button.grid(row=60, column=0, pady=20)
admin_delete = tk.Frame(admin_window, width=100, height=50)
admin_delete.grid(row=55, column=10, padx=50, pady=50)
self.admin_delete_Image = tk.PhotoImage(file="images\\delete2.png")
self.admin_delete_Image_Pack = tk.Label(admin_delete, image=self.admin_delete_Image)
self.admin_delete_Image_Pack.grid()
self.delete_staff_button = tk.Button(admin_window, text="Remove Staff", width=15, font=("Helvetica", 12))
self.delete_staff_button.grid(row=60, column=10, pady=20)
admin_select = tk.Frame(admin_window, width=100, height=50)
admin_select.grid(row=55, column=20, padx=50, pady=50)
self.admin_select_Image = tk.PhotoImage(file="images\\select2.png")
self.admin_select_Image_Pack = tk.Label(admin_select, image=self.admin_select_Image)
self.admin_select_Image_Pack.grid()
self.select_staff_button = tk.Button(admin_window, text="Select Staff", width=15, font=("Helvetica", 12))
self.select_staff_button.grid(row=60, column=20, pady=20)
admin_display = tk.Frame(admin_window, width=100, height=50)
admin_display.grid(row=55, column=30, padx=50, pady=50)
self.admin_display_Image = tk.PhotoImage(file="images\\display1.png")
self.admin_display_Image_Pack = tk.Label(admin_display, image=self.admin_display_Image)
self.admin_display_Image_Pack.grid()
self.display_staff_button = tk.Button(admin_window, text="Display Staff", width=15, font=("Helvetica", 12))
self.display_staff_button.grid(row=60, column=30, pady=20)
class StaffPage(BasePage):
def __init__(self,parent,controller):
super().__init__(parent, controller)
staff_window = tk.Frame(self, width=200, height=100, background="white", highlightbackground="black",highlightthickness=1)
staff_window.grid(row=50, column=150, padx=300, pady=100)
self.label_title_staff = tk.Label(staff_window, text="Welcome Staff", font=("Helvetica", 40), bg="white")
self.label_title_staff.grid(row=0, column=0, padx=10, pady=10, sticky='W')
staff_register = tk.Frame(staff_window, width=100, height=50)
staff_register.grid(row=55, column=0, padx=50, pady=50)
self.register_Image = tk.PhotoImage(file="images\\register1.png")
self.register_Image_Pack = tk.Label(staff_register, image=self.register_Image)
self.register_Image_Pack.grid()
self.register_staff_button = tk.Button(staff_window, text="Add Details", width=15, font=("Helvetica", 12))
self.register_staff_button.grid(row=60, column=0, pady=20)
staff_display = tk.Frame(staff_window, width=100, height=50)
staff_display.grid(row=55, column=30, padx=50, pady=50)
self.staff_display_Image = tk.PhotoImage(file="images\\display1.png")
self.staff_display_Image_Pack = tk.Label(staff_display, image=self.staff_display_Image)
self.staff_display_Image_Pack.grid()
self.display_staff_button = tk.Button(staff_window, text="Display Details", width=15, font=("Helvetica", 12))
self.display_staff_button.grid(row=60, column=30, pady=20)
app = SchoolApp()
app.mainloop()
This is my log in page code
class LogIn(BasePage):
def __init__(self,parent,controller):
super().__init__(parent, controller)
login_window = tk.Frame(self, width=200, height=100, background="white")
login_window.grid(row=50, column=150, padx=300, pady=100)
login_img_frame = tk.Frame(login_window, width=200, height=50, background="white")
login_img_frame.grid(row=55, column=0, padx=50, pady=50)
self.login_Image = tk.PhotoImage(file="images\\lock3.png")
self.login_Image_Pack = tk.Label(login_img_frame, image=self.login_Image)
self.login_Image_Pack.grid()
login_frame = tk.Frame(login_window, width=100, height=50, background="white", highlightbackground="black",highlightthickness=1)
login_frame.grid(row=55, column=10, padx=50, pady=50)
self.label_title = tk.Label(login_frame, text="Log In", font=("Helvetica", 40), bg="white")
self.label_title.grid(row=0, column=20, padx=10, pady=10)
self.label_username = tk.Label(login_frame, text="Username", font=("Helvetica", 20), bg="white")
self.label_username.grid(row=50, column=20, padx=10, pady=10)
self.entry_username = tk.Entry(login_frame, width=15, font=("Helvetica", 20), bd=3)
self.entry_username.grid(row=50, column=30, padx=10, pady=10)
self.label_password = tk.Label(login_frame, text="Password", font=("Helvetica", 20), bg="white")
self.label_password.grid(row=60, column=20, padx=10, pady=10)
self.entry_password = tk.Entry(login_frame, width=15, font=("Helvetica", 20), bd=3)
self.entry_password.grid(row=60, column=30, padx=10, pady=10)
#print(self.entry_username.get())
if(self.entry_username.get()=="admin"):
self.login_button = tk.Button(login_frame, text="Log In", command=lambda:[self.submit(),controller.show_frame(AdminPage)],font=("Helvetica", 20), bg="white")
self.login_button.grid(row=70, column=25, padx=10, pady=10)
else:
self.login_button = tk.Button(login_frame, text="Log In", command=lambda:[self.submit(),controller.show_frame(StaffPage)],font=("Helvetica", 20), bg="white")
self.login_button.grid(row=70, column=25, padx=10, pady=10)
def submit(self):
self.u_name = self.entry_username.get()
self.p_word = self.entry_password.get()
employee = mysql.connector.connect(host="localhost", user="root", password="", database="edatabase")
cursor_variable = employee.cursor()
cursor_variable.execute("INSERT INTO login VALUES ('" + self.u_name + "','" + self.p_word + "')")
employee.commit()
employee.close()
I have no errors. Whether it's admin or staff the else condition works and staff page is getting displayed.
Any help is highly appreciated.

I modified the class LogIn(BasePage) as follows and it works. I changed the submit(self) to submit(self,controller).
Thank you everyone for your response.
class LogIn(BasePage):
def __init__(self,parent,controller):
super().__init__(parent, controller)
login_window = tk.Frame(self, width=200, height=100, background="white")
login_window.grid(row=50, column=150, padx=300, pady=100)
login_img_frame = tk.Frame(login_window, width=200, height=50, background="white")
login_img_frame.grid(row=55, column=0, padx=50, pady=50)
self.login_Image = tk.PhotoImage(file="images\\lock3.png")
self.login_Image_Pack = tk.Label(login_img_frame, image=self.login_Image)
self.login_Image_Pack.grid()
login_frame = tk.Frame(login_window, width=100, height=50, background="white", highlightbackground="black",highlightthickness=1)
login_frame.grid(row=55, column=10, padx=50, pady=50)
self.label_title = tk.Label(login_frame, text="Log In", font=("Helvetica", 40), bg="white")
self.label_title.grid(row=0, column=20, padx=10, pady=10)
self.label_username = tk.Label(login_frame, text="Username", font=("Helvetica", 20), bg="white")
self.label_username.grid(row=50, column=20, padx=10, pady=10)
self.entry_username = tk.Entry(login_frame, width=15, font=("Helvetica", 20), bd=3)
self.entry_username.grid(row=50, column=30, padx=10, pady=10)
self.label_password = tk.Label(login_frame, text="Password", font=("Helvetica", 20), bg="white")
self.label_password.grid(row=60, column=20, padx=10, pady=10)
self.entry_password = tk.Entry(login_frame, width=15, font=("Helvetica", 20), bd=3)
self.entry_password.grid(row=60, column=30, padx=10, pady=10)
#print(self.entry_username.get())
self.login_button = tk.Button(login_frame, text="Log In", command=lambda:self.submit(controller),font=("Helvetica", 20), bg="white")
self.login_button.grid(row=70, column=25, padx=10, pady=10)
def submit(self,controller):
self.u_name = self.entry_username.get()
self.p_word = self.entry_password.get()
employee = mysql.connector.connect(host="localhost", user="root", password="", database="edatabase")
cursor_variable = employee.cursor()
cursor_variable.execute("INSERT INTO login VALUES ('" + self.u_name + "','" + self.p_word + "')")
employee.commit()
employee.close()
if (self.u_name== "admin"):
controller.show_frame(AdminPage)
else:
controller.show_frame(StaffPage)

Related

Tkinter frame not showing on button click

I am trying to develop a school app in tkinter. I am creating a frame for each tab in the app. I have included my tabs as pages in the code. I have created the UI for each tab. It works perfectly fine when run individually.
I tried to combine it by stacking the frames.
I am having no error but the tkinter frame corresponding to the 'Home' in the app is not loading.
Any help is appreciated.
class SchoolApp(tk.Tk):
def __init__(self,*args, **kwargs):
super().__init__(*args, **kwargs)
self.geometry("1500x750")
main_frame = tk.Frame(self,width=200,height=50,highlightbackground="black",highlightthickness=1)
main_frame.pack(side='top', fill='both', expand='True')
main_frame.grid_rowconfigure(0, weight=1)
main_frame.grid_columnconfigure(0, weight=1)
self.frames = {}
pages = (Home,HomePage,LogIn,AdminPage,StaffPage,AdminAddStaff,AdminRemoveStaff,AdminSelectStaff,AdminSearchStaff,AdminUpdateStaff,StaffAddDetails,StaffDisplyDetails)
for F in pages:
frame = F(main_frame, self)
self.frames[F] = frame
frame.pack()
class BasePage(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
#label_bkgr = tk.Label(self, image=controller.bkgr_image)
#label_bkgr.place(x=0,y=0) # Center label w/image.
pass
class Home(BasePage):
def __init__(self,parent,controller):
super().__init__(parent, controller)
home_frame = tk.Frame(self, width=200, height=50, highlightbackground="black", highlightthickness=1)
home_frame.pack(side='top', fill='both', expand='True')
p1 = tk.PanedWindow(home_frame, orient='horizontal', bd=4, relief='raised', bg='#595959', width=1500)
p1.pack()
home = tk.Button(p1, text='Home', height=1, width=15, font=("Helvetica", 15), anchor='center')
p1.add(home)
about = tk.Button(p1, text='About Us', height=1, width=15, font=("Helvetica", 15), anchor='center')
p1.add(about)
academic = tk.Button(p1, text='Academic', height=1, width=15, font=("Helvetica", 15), anchor='center')
p1.add(academic)
facilities = tk.Button(p1, text='Facilities', height=1, width=15, font=("Helvetica", 15), anchor='center')
p1.add(facilities)
student_life = tk.Button(p1, text='Student Life', height=1, width=15, font=("Helvetica", 15), anchor='center')
p1.add(student_life)
contact = tk.Button(p1, text='Contact', height=1, width=15, font=("Helvetica", 15), anchor='center')
p1.add(contact)
admin_portal = tk.Button(p1, text='Admin Portal', height=1, width=15, font=("Helvetica", 15), anchor='center')
p1.add(admin_portal)
staff_portal = tk.Button(p1, text='Staff Portal', height=1, width=15, font=("Helvetica", 15), anchor='center')
p1.add(staff_portal)
l = tk.Label(home_frame, text="Fact of the Day")
l.pack()
T = tk.Text(home_frame, height=5, width=52, font='Arial')
T.pack()
Fact = """A man can be arrested in Italy for wearing a skirt in public."""
T.insert(tk.END, Fact)
def show_frame(self,container):
frame = self.frames[container]
frame.tkraise()

How to get date from tkcalendar in tkinter gui?

I am trying to create an employee web app. I have created the home page, login page and application page. The application page gets the details of the candidates who want to apply for a job.
I have used tkcalendar module for selecting 'expected start date'. The GUI works fine but I am not able to get the value of the date selected.
I tried to use get_date() but it shows the following error:
self.date = self.choose_date.get_date()
AttributeError: 'Button' object has no attribute 'get_date'
This is the code I executed.
import tkinter as tk
from tkinter import ttk
import mysql.connector
from tkcalendar import *
class ABCApp(tk.Tk):
BKGR_IMAGE_PATH = 'images\\bg5.png'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.geometry("1500x750")
main_frame = tk.Frame(self,width=200,height=50,highlightbackground="black",highlightthickness=1,background = "#e6ffe6")
main_frame.pack(side='top', fill='both', expand='True')
main_frame.grid_rowconfigure(0, weight=1)
main_frame.grid_columnconfigure(0, weight=1)
self.bkgr_image = tk.PhotoImage(file=self.BKGR_IMAGE_PATH)
self.frames = {}
for F in (HomePage,LogIn,PersonalPage,ApplicationPage):
frame = F(main_frame, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky='nsew')
self.show_frame(HomePage)
def show_frame(self,container):
frame = self.frames[container]
frame.tkraise()
class BasePage(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
label_bkgr = tk.Label(self, image=controller.bkgr_image)
label_bkgr.place(x=0,y=0) # Center label w/image.
class HomePage(BasePage):
def __init__(self, parent, controller):
super().__init__(parent, controller)
label = ttk.Label(self, text='Home Page', font =("Helvetica",20))
label.pack(padx=10, pady=10)
button1 = ttk.Button(self, text="Log In",
command=lambda: controller.show_frame(LogIn))
button1.pack()
class LogIn(BasePage):
def __init__(self,parent,controller):
super().__init__(parent, controller)
login_frame = tk.Frame(self, width=200, height=50, background="white")
login_frame.grid(row=400, column=20, padx=500, pady=250)
self.label_title = tk.Label(login_frame, text="Log In", font=("Helvetica", 40), bg="white")
self.label_title.grid(row=0, column=20, padx=10, pady=10)
self.label_username = tk.Label(login_frame, text="Username", font=("Helvetica", 20), bg="white")
self.label_username.grid(row=50, column=20, padx=10, pady=10)
self.entry_username = tk.Entry(login_frame, width=15, font=("Helvetica", 20), bd=3)
self.entry_username.grid(row=50, column=30, padx=10, pady=10)
self.label_password = tk.Label(login_frame, text="Password", font=("Helvetica", 20), bg="white")
self.label_password.grid(row=60, column=20, padx=10, pady=10)
self.entry_password = tk.Entry(login_frame, width=15, font=("Helvetica", 20), bd=3)
self.entry_password.grid(row=60, column=30, padx=10, pady=10)
self.login_button = tk.Button(login_frame, text="Log In",command=lambda: [self.submit(),controller.show_frame(PersonalPage)],font=("Helvetica", 20),bg="white")
self.login_button.grid(row=70, column=25, padx=10, pady=10)
def submit(self):
self.u_name = self.entry_username.get()
self.p_word = self.entry_password.get()
employee = mysql.connector.connect(host="localhost", user="root", password="", database="edatabase")
cursor_variable = employee.cursor()
cursor_variable.execute("INSERT INTO login VALUES ('" + self.u_name + "','" + self.p_word + "')")
employee.commit()
employee.close()
class PersonalPage(BasePage):
def __init__(self, parent, controller):
super().__init__(parent, controller)
personal_frame = tk.Frame(self, width=200, height=100, background="white")
personal_frame.grid(row=50, column=150, padx=300, pady=100)
personal_details_frame = tk.Frame(personal_frame,width =150,height =50,background="#e6ffe6")
personal_details_frame.grid(row =55,column =0,padx = 50,pady = 50)
self.label_title = tk.Label(personal_details_frame, text="Personal Details",font=("Helvetica", 20),bg ="#e6ffe6")
self.label_title.grid(row=10, column=0, sticky='W')
self.label_name = tk.Label(personal_details_frame, text="Name",font=("Helvetica", 12),bg ="#e6ffe6")
self.label_name.grid(row=15, column=0, padx=10, pady=10, sticky='W')
self.entry_name = tk.Entry(personal_details_frame,bd=3, font=("Helvetica", 12), width=50)
self.entry_name.grid(row=15, column=10, padx=10, pady=10, sticky='W')
self.label_email = tk.Label(personal_details_frame, text="Email id", font=("Helvetica", 12),bg ="#e6ffe6")
self.label_email.grid(row=30, column=0, padx=10, pady=10, sticky='W')
self.entry_email = tk.Entry(personal_details_frame, bd=3, font=("Helvetica", 12), width=50)
self.entry_email.grid(row=30, column=10, padx=10, pady=10, sticky='W')
self.label_mobilenumber = tk.Label(personal_details_frame, text="Mobile Number",font=("Helvetica", 12),bg ="#e6ffe6")
self.label_mobilenumber.grid(row=45, column=0, padx=10, pady=10, sticky='W')
self.entry_mobile = tk.Entry(personal_details_frame,bd=3, font=("Helvetica", 12), width=50)
self.entry_mobile.grid(row=45, column=10, padx=10, pady=10, sticky='W')
self.label_address = tk.Label(personal_details_frame, text="Address",font=("Helvetica", 12),bg ="#e6ffe6")
self.label_address.grid(row=60, column=0, padx=10, pady=10, sticky='W')
self.text_address = tk.Text(personal_details_frame,bd=3, font=("Helvetica", 12), width=50, height=5)
self.text_address.grid(row=60, column=10, padx=10, pady=10, sticky='W')
self.button_next = tk.Button(personal_details_frame,text = "Next",font = ("Helvetica",12),bg ="#b3ccff",command=lambda: [self.personal_next(),controller.show_frame(ApplicationPage)])
self.button_next.grid(row = 70,column =20,padx=10,pady=10,sticky='W')
def personal_next(self):
self.name = self.entry_name.get()
self.email = self.entry_email.get()
self.mobile = self.entry_mobile.get()
self.address = self.text_address.get("1.0","end-1c")
employee = mysql.connector.connect(host="localhost", user="root", password="", database="edatabase")
cursor_variable = employee.cursor()
cursor_variable.execute("INSERT INTO personal VALUES ('" + self.name + "','" + self.email + "','" + self.mobile + "','" + self.address + "')")
employee.commit()
employee.close()
class ApplicationPage(BasePage):
def __init__(self, parent, controller):
super().__init__(parent, controller)
application_frame = tk.Frame(self, width=200, height=100, background="white")
application_frame.grid(row=50, column=150, padx=300, pady=100)
application_details_frame = tk.Frame(application_frame, width=150, height=50, background="#e6ffe6")
application_details_frame.grid(row=55, column=0, padx=50, pady=50)
self.label_title = tk.Label(application_details_frame, text="Application Details", font=("Helvetica", 20),bg="#e6ffe6")
self.label_title.grid(row=10, column=0, sticky='W')
self.label_position = tk.Label(application_details_frame, text="Applied for Position", font=("Helvetica", 12),bg ="#e6ffe6")
self.label_position.grid(row=75, column=0, padx=10, pady=10, sticky='W')
self.list_box1 = tk.Listbox(application_details_frame, width=50, height=3, font=("Helvetica", 12))
self.list_box1.insert(1, "Database Admin")
self.list_box1.insert(2, "Network Admin")
self.list_box1.insert(3, "Deployment Admin")
self.list_box1.grid(row=75, column=10, padx=10, pady=10, sticky='w')
self.label_date = tk.Label(application_details_frame, text="Expected Start Date", font=("Helvetica", 12),bg ="#e6ffe6")
self.label_date.grid(row=90, column=0, padx=10, pady=10, sticky='W')
self.choose_date = tk.Button(application_details_frame, text="Choose Available Date", command=lambda: self.open_calendar(), width=49, bg="white",font=("Helvetica", 12), anchor='w')
self.choose_date.grid(row=90, column=10, padx=10, pady=10, sticky='w')
self.label_proficiency = tk.Label(application_details_frame, text="Proficient Language", font=("Helvetica", 12),bg ="#e6ffe6")
self.label_proficiency.grid(row=120, column=0, padx=10, pady=10, sticky='W')
self.radio = tk.IntVar()
self.radio_button1 = tk.Radiobutton(application_details_frame, text="Java", variable=self.radio, value=1, font=("Helvetica", 12),bg ="#e6ffe6")
self.radio_button1.grid(row=120, column=10, padx=10, pady=10, sticky='w')
self.radio_button2 = tk.Radiobutton(application_details_frame, text="SQL", variable=self.radio, value=2, font=("Helvetica", 12),bg ="#e6ffe6")
self.radio_button2.grid(row=135, column=10, padx=10, pady=10, sticky='w')
self.radio_button3 = tk.Radiobutton(application_details_frame, text="C", variable=self.radio, value=3, font=("Helvetica", 12),bg ="#e6ffe6")
self.radio_button3.grid(row=150, column=10, padx=10, pady=10, sticky='w')
self.submit_button = tk.Button(application_details_frame,text = "Submit",command = self.submit(),font = ("Helvetica",12),bg ="#b3ccff")
self.submit_button.grid(row = 220,column =10,padx=10,pady=10,sticky='W')
def submit(self):
self.position = self.list_box1.get(tk.ANCHOR)
self.date = self.choose_date.get_date()
employee = mysql.connector.connect(host="localhost", user="root", password="", database="edatabase")
cursor_variable = employee.cursor()
cursor_variable.execute("INSERT INTO application VALUES ('" + self.position + "','"+self.date+"')")
employee.commit()
employee.close()
def open_calendar(self):
self.toplevel1 = tk.Toplevel(self)
self.calendar_variable = Calendar(self.toplevel1, selectmode="day", year=2021, month=4, day=10)
self.calendar_variable.pack()
self.toplevel1.mainloop()
app = ABCApp()
app.mainloop()
Any help is appreciated.
First the following line:
self.submit_button = tk.Button(application_details_frame,text = "Submit",command = self.submit(),font = ("Helvetica",12),bg ="#b3ccff")
will execute self.submit() immediately. It should be:
self.submit_button = tk.Button(application_details_frame, text="Submit", command=self.submit, font=("Helvetica",12), bg="#b3ccff")
Second as the error said, self.choose_date is a tk.Button not tkcalendar.Calendar. Better create an instance variable of tk.StringVar and associate it to the tkcalendar.Calendar widget. Then you can use this instance variable to get the selected date:
class ApplicationPage(BasePage):
def __init__(self, parent, controller):
...
self.selected_date = tk.StringVar()
def submit(self):
self.position = self.list_box1.get(tk.ANCHOR)
self.date = self.selected_date.get() # get the selected date
...
def open_calendar(self):
...
self.calendar_variable = Calendar(self.toplevel1, selectmode="day", year=2021, month=4, day=10,
textvariable=self.selected_date)
...

How to run function in other class when i press button in other class

im making a program that will make my mother accounting more easily.
so i still had this problem to run function that will insert data to treeview. and the button in other class.
sry im newbie here :D
i already tried make function in local class that call the class with treeview, but it still give me error init() missing 1 required positional argument: 'master'
this my class with treeview and function that will insert data into tree view :
class windowHitung():
def __init__(self, master):
self.master=master
self.data = dataHolder()
self.w1=tk.Toplevel(self.master)
self.w1.wm_title("Program Hitung Untung dari modal")
self.frameMenuHitung=Frame(self.w1, width=100, height=50)
self.frameMenuHitung.grid(row=0, column=0)
self.treeItems = ttk.Treeview(self.frameMenuHitung, columns=("nama", "jumlah", "harga"))
self.treeItems['show'] = 'headings'
self.treeItems.column('nama', anchor=N)
self.treeItems.column('jumlah', anchor=N)
self.treeItems.column('harga', anchor=N)
self.treeItems.heading('nama', text="Nama Barang")
self.treeItems.heading('jumlah', text="Jumlah")
self.treeItems.heading('harga', text="Harga")
self.treeItems.grid(row=0, column=0)
self.itemscroll = ttk.Scrollbar(self.frameMenuHitung, command=self.treeItems.yview)
self.itemscroll.grid(row=0, column=1, sticky='nsw')
self.treeItems.config(yscrollcommand=self.itemscroll.set)
self.labelModal = Label(self.frameMenuHitung, text="Modal ")
self.labelModal.grid(row=1, column=0, sticky='w', padx=5)
self.entryModal = Entry(self.frameMenuHitung)
self.entryModal.grid(row=1, column=0, sticky='n')
self.buttonTambah = ttk.Button(self.frameMenuHitung, text="Tambah", command=self.barangInputShow)
self.buttonTambah.grid(row=2, column=0, padx=5, pady=5, sticky='w')
self.buttonEdit = ttk.Button(self.frameMenuHitung, text="Edit")
self.buttonEdit.grid(row=2, column=0, padx=5, pady=5, sticky='n')
self.buttonBuang = ttk.Button(self.frameMenuHitung, text="Buang")
self.buttonBuang.grid(row=2, column=0, padx=5, pady=5, sticky='e')
def insertValue(self):
self.treeItems.insert("", END, values=(dataHolder.namaBarang,"",""))
def barangInputShow(self):
inputBarang(self.master)
and this the toplevel that will be inputdata for the treeview.
class inputBarang(windowHitung):
def __init__(self, master):
self.master = master
self.windowHitung = windowHitung()
self.inputBarang = Toplevel(self.master)
self.inputBarang.title("Input Item Properties")
self.frameInput = Frame(self.inputBarang)
self.frameInput.grid(row=0, column=0)
self.labelBarang = Label(self.frameInput, text="Nama Barang")
self.labelBarang.grid(row=0, column=0, sticky='w', padx=5, pady=6)
self.entryBarang = Entry(self.frameInput)
self.entryBarang.grid(row=0, column=1, sticky='w', padx=3, pady=6)
self.labelJumlah = Label(self.frameInput, text="Jumlah Barang")
self.labelJumlah.grid(row=1, column=0, sticky='w', padx=5, pady=6)
self.entryJumlah = Entry(self.frameInput)
self.entryJumlah.grid(row=1, column=1, sticky='w', padx=3, pady=6)
self.labelHarga = Label(self.frameInput, text="Harga")
self.labelHarga.grid(row=2, column=0, sticky='w', padx=5, pady=6)
self.entryHarga = Entry(self.frameInput)
self.entryHarga.grid(row=2, column=1, sticky='w', padx=3, pady=6)
self.buttonInsertBarang = Button(self.frameInput, text="Tambah barang", width=20, height=5, command=self.windowHitung.insertValue)
self.buttonInsertBarang.grid(row=3, columnspan=2, sticky="n")
if you need full code. :
from tkinter import *
from tkinter import ttk, messagebox
import tkinter as tk
from Hitung import *
import Hitung as cnt
class mainWindow():
def keluar():
pesanKeluar = messagebox.askyesno("Alert", "Apakah anda ingin keluar?")
if pesanKeluar == True:
root.destroy()
else:
pass
def __init__(self, master):
self.master=master
self.frameMainMenu=Frame(master, width=100, height=50)
self.frameMainMenu.pack()
self.buttonHitung=Button(self.frameMainMenu, text="Hitung", command=self.windowHitungShow)
self.buttonHitung.grid(row=0, padx=5, pady=2)
self.buttonLihatRiwayat=Button(self.frameMainMenu, text="Riwayat")
self.buttonLihatRiwayat.grid(row=1, padx=5, pady=2)
self.buttonKeluar=Button(self.frameMainMenu, text="Keluar", command=mainWindow.keluar)
self.buttonKeluar.grid(row=2, padx=5, pady=2)
def windowHitungShow(self):
windowHitung(self.master)
class dataHolder():
namaBarang="test"
jumlahBarang=0
hargaBarang=00
def __init__(self):
dataHolder.namaBarang="test"
dataHolder.jumlahBarang=0
dataHolder.hargaBarang=0
class windowHitung():
def __init__(self, master):
self.master=master
self.data = dataHolder()
self.w1=tk.Toplevel(self.master)
self.w1.wm_title("Program Hitung Untung dari modal")
self.frameMenuHitung=Frame(self.w1, width=100, height=50)
self.frameMenuHitung.grid(row=0, column=0)
self.treeItems = ttk.Treeview(self.frameMenuHitung, columns=("nama", "jumlah", "harga"))
self.treeItems['show'] = 'headings'
self.treeItems.column('nama', anchor=N)
self.treeItems.column('jumlah', anchor=N)
self.treeItems.column('harga', anchor=N)
self.treeItems.heading('nama', text="Nama Barang")
self.treeItems.heading('jumlah', text="Jumlah")
self.treeItems.heading('harga', text="Harga")
self.treeItems.grid(row=0, column=0)
self.itemscroll = ttk.Scrollbar(self.frameMenuHitung, command=self.treeItems.yview)
self.itemscroll.grid(row=0, column=1, sticky='nsw')
self.treeItems.config(yscrollcommand=self.itemscroll.set)
self.labelModal = Label(self.frameMenuHitung, text="Modal ")
self.labelModal.grid(row=1, column=0, sticky='w', padx=5)
self.entryModal = Entry(self.frameMenuHitung)
self.entryModal.grid(row=1, column=0, sticky='n')
self.buttonTambah = ttk.Button(self.frameMenuHitung, text="Tambah", command=self.barangInputShow)
self.buttonTambah.grid(row=2, column=0, padx=5, pady=5, sticky='w')
self.buttonEdit = ttk.Button(self.frameMenuHitung, text="Edit")
self.buttonEdit.grid(row=2, column=0, padx=5, pady=5, sticky='n')
self.buttonBuang = ttk.Button(self.frameMenuHitung, text="Buang")
self.buttonBuang.grid(row=2, column=0, padx=5, pady=5, sticky='e')
def insertValue(self):
self.treeItems.insert("", END, values=(dataHolder.namaBarang,"",""))
def barangInputShow(self):
inputBarang(self.master)
class inputBarang(windowHitung):
def __init__(self, master):
self.master = master
self.windowHitung = windowHitung()
self.inputBarang = Toplevel(self.master)
self.inputBarang.title("Input Item Properties")
self.frameInput = Frame(self.inputBarang)
self.frameInput.grid(row=0, column=0)
self.labelBarang = Label(self.frameInput, text="Nama Barang")
self.labelBarang.grid(row=0, column=0, sticky='w', padx=5, pady=6)
self.entryBarang = Entry(self.frameInput)
self.entryBarang.grid(row=0, column=1, sticky='w', padx=3, pady=6)
self.labelJumlah = Label(self.frameInput, text="Jumlah Barang")
self.labelJumlah.grid(row=1, column=0, sticky='w', padx=5, pady=6)
self.entryJumlah = Entry(self.frameInput)
self.entryJumlah.grid(row=1, column=1, sticky='w', padx=3, pady=6)
self.labelHarga = Label(self.frameInput, text="Harga")
self.labelHarga.grid(row=2, column=0, sticky='w', padx=5, pady=6)
self.entryHarga = Entry(self.frameInput)
self.entryHarga.grid(row=2, column=1, sticky='w', padx=3, pady=6)
self.buttonInsertBarang = Button(self.frameInput, text="Tambah barang", width=20, height=5, command=self.windowHitung.insertValue)
self.buttonInsertBarang.grid(row=3, columnspan=2, sticky="n")
if __name__=="__main__":
root=Tk()
root.wm_title("Program Hitung Untung dari modal")
#root.geometry("200x200")
main = mainWindow(root)
root.mainloop()
thanks to see my request
i expect to make the button run the funcion, but it still give me errors

How Can I write Python code for Temperature Convert to C orF using Frames, Entry Box and Label?

from tkinter import*
def convert2c():
try:
f = float(tempIN.get())
c = (f-32)*5.0/9.0
tempOUTc.set(c)
except ValueError:
pass
def convert2f():
try:
c = float(tempIN.get())
f = c*9.0/5.0 + 32
tempOUTf.set(f)
except ValueError:
pass
root = Tk()
f = root
root.title("Temperature Converter")
root.geometry('{}x{}'.format(500,200))
topFrm = Frame(root, width=500, height=80, bg="#e01111")
midFrm = Frame(root, width=500, height=150, bg="#795fb5")
btmFrm = Frame(root, width=500, height=100)
btmL = Frame(btmFrm, width=250, height=100, bg="#4117a5")
btmR = Frame(btmFrm, width=250, height=100, bg="#3f454f")
topFrm.grid(row=0, sticky="W")
midFrm.grid(row=1)
btmFrm.grid(row=3)
btmL.grid(row=2, column=0)
btmR.grid(row=2, column=2)
tempIN = StringVar()
tempOUTc = StringVar()
tempOUTf = StringVar()
Label(f, text="Temperature Converter\n A number in temperature degree is converted into another as one of the button is clicked").grid(column=0, row=0)
Label(f, text="Enter a Temperature").grid(column=0, row=1)
Entry(f, textvariable=tempIN, width=10) .grid(column=0, rowspan=1, row=1, sticky=W)
Button(f, text=" to Celsius", command=convert2c) .grid(column=0, row=3, sticky=W)
Button(f, text=" to Fahrenheit", command=convert2f) .grid(column=1, row=3, sticky=W)
Label(f, textvariable=tempOUTc) .grid(column=2, row=1, sticky=W)
Label(f, textvariable=tempOUTf) .grid(column=2, row=2, sticky=W)
root.mainloop()

Having trouble with this method of changing and showing a new window in python

I am having quite the trouble using classes in python. The first code I used in changing and showing a new window works but too cluttered so i decided to use the class. but there might be something wrong in my code. Can you atleast see to it? Thank you. Hoping for a fast response.
import random, sys, os, rabinMiller, cryptomath
from tkinter import *
import tkinter as tk
from PIL import Image, ImageTk
from tkinter.filedialog import askopenfile
class MainFrame(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
#frame1.title("RSA Cryptosystem")
#frame1.config(bg="black")
container.columnconfigure(0, weight=1)
container.rowconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, DecryptionPage, EncryptionPage, MakeKeyPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky=(N, W, E, S))
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
image4 = Image.open("rsa cryptosystem logo.jpg")
photo4 = ImageTk.PhotoImage(image4)
label1 = tk.Label(self, image=photo4)
label1.grid(column=1, row=1, columnspan=3, padx=5, pady=5)
label1.image = photo4
image1 = Image.open("MAKE KEY.jpg")
photo1 = ImageTk.PhotoImage(image1)
image2 = Image.open("encryption button.jpg")
photo2 = ImageTk.PhotoImage(image2)
image3 = Image.open("decryption button.jpg")
photo3 = ImageTk.PhotoImage(image3)
button1 = tk.Button(self, compound=TOP, command=lambda: controller.show_frame(MakeKeyPage), width=338, height=338, image=photo1)
button1.grid(column=1, row=2, sticky=S, rowspan=2, padx=5, pady=5)
button1.image = photo1
button2 = tk.Button(self, compound=TOP, command=lambda: controller.show_frame(EncryptionPage), width=525, height=150, image=photo2)
button2.grid(column=2, row=2, sticky=(W, E), columnspan=2, padx=5, pady=5)
button2.image = photo2
button3 = tk.Button(self, compound=TOP, command=lambda: controller.show_frame(DecryptionPage), width=525, height=150, image=photo3)
button3.grid(column=2, row=3, sticky=(W, E), columnspan=2, padx=5, pady=5)
button3.image = photo3
class DecryptionPage(tk.Frame):
def __init__(self, parent, controller):
textframe1 = tk.Text(self, width=70, height=32).grid(column=3, row=2, rowspan=3, padx=5, pady=5, sticky=(S, E))
image5 = Image.open("INSERT PRIVATE KEY.jpg")
photo5 = ImageTk.PhotoImage(image5)
image6 = Image.open("INSERT ENCRYPTED TEXT.jpg")
photo6 = ImageTk.PhotoImage(image6)
image7 = Image.open("DECRYPT.jpg")
photo7 = ImageTk.PhotoImage(image7)
button4 = tk.Button(self, width=338, height=80, image=photo5, command=open_file)
button4.grid(column=1, row=2,columnspan=2, sticky=N)
button4.image = photo5
button5 = tk.Button(self, width=338, height=80, image=photo6, command=open_file)
button5.grid(column=1, row=3, columnspan=2, sticky=N)
button5.image = photo6
button6 = tk.Button(self, text="BACK", command=lambda: controller.show_frame(StartPage))
button6.grid(column=1, row=1, padx=5, pady=5, sticky=W)
button7 = tk.Button(self, width=338, height=338, image=photo7)
button7.grid(column=1, row=4, padx=5, pady=5, columnspan=2, sticky=S)
button7.image = photo7
class EncryptionPage(tk.Frame):
def __init__(self, parent, controller):
textframe2 = tk.Text(self, width=70, height=32).grid(column=3, row=2, columnspan=2, rowspan=3, padx=5, pady=5, sticky=(S, E))
image8 = Image.open("INSERT PUBLIC KEY.jpg")
photo8 = ImageTk.PhotoImage(image8)
image9 = Image.open("ENCRYPT.jpg")
photo9 = ImageTk.PhotoImage(image9)
button8 = tk.Button(self, width=338, height=80, image=photo8, command=open_file)
button8.grid(column=1, row=2,columnspan=2, sticky=N)
button8.image = photo8
button10 = tk.Button(self, text="BACK", command=lambda: controller.show_frame(StartPage))
button10.grid(column=1, row=1, padx=5, pady=5, sticky=W)
button9 = tk.Button(self, width=338, height=338, image=photo9)
button9.grid(column=1, row=4, padx=5, pady=5, columnspan=2, sticky=S)
button9.image = photo9
receivename = StringVar()
receivename_entry = tk.Entry(self, width=85, textvariable=receivename).grid(column=4, row=1, padx=5, pady=5, sticky=(W, E))
label4 = tk.Label(self, text="TO: ").grid(column=3, row=1, padx=5, pady=5, sticky=W)
class MakeKeyPage(tk.Frame):
def __init__(self, parent, controller):
keyname = StringVar()
phone = StringVar()
keyname_entry = tk.Entry(self, width=15, textvariable=keyname)
keyname_entry.grid(column=2, row=2, sticky=E, padx=5, pady=5)
label2 = tk.Label(self, text="Key Name: ")
label2.grid(column=1, row=2, sticky=W, padx=5, pady=5)
label3 = tk.Label(self, text="Key Type: ")
label3.grid(column=1, row=3, sticky=W, padx=5, pady=5)
button12 = tk.Button(self, text="BACK", command=lambda: controller.show_frame(StartPage)).grid(column=1, row=1, padx=5, pady=5, sticky=W)
prime = tk.Radiobutton(self, text="Prime", variable=phone, value='prime').grid(column=1, row=4, sticky=(W, E))
luckyprime = tk.Radiobutton(self, text="Lucky Prime", variable=phone, value='luckyprime').grid(column=2, row=4, sticky=(W, E))
button11 = tk.Button(self, text="Generate Keys").grid(column=1, row=5, columnspan=2, sticky=(W, E))
app = MainFrame()
app.mainloop()

Resources