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)
...
Related
I want to activate the second entry when the checkbox is checked...but the it works the other way around. What am I doing wrong? Based on another question I have posted it seems the event "<ButtonRelease>" occurs before the bonding. Why is that? Can I use "command" in the checkbox instaed?
import tkinter as tk
def set_entry_status(event, var, widg):
print(var.get())
if var.get():
widg[-1]['state'] = 'normal'
else:
widg[-1]['state'] = 'disabled'
def CustomWidget(frame, name, unit, ):
var_e = []
widget_e = []
var_c = tk.IntVar(master=frame, value=0)
widget_c = tk.Checkbutton(master=frame, text='', variable=var_c)
widget_c.grid(row=0, column=0, columnspan=1, padx=5, pady=5, sticky="ns")
label_l = name + " (" + unit + ")" # nome + unità di misura in parentesi per GUI
widget_l = tk.Label(frame, text=label_l, padx=1, pady=1)
widget_l.grid(row=0, column=1, columnspan=1, padx=5, pady=5, sticky="wns")
var_e.append(tk.StringVar(master=frame, value='A'))
widget_e.append(tk.Entry(frame, textvariable=var_e[-1], width=10, state="normal"))
widget_e[-1].grid(row=0, column=2, columnspan=1, padx=5, pady=5, sticky="ns")
var_e.append(tk.StringVar(master=frame, value='B'))
widget_e.append(tk.Entry(frame, textvariable=var_e[-1], width=10, state="normal"))
widget_e[-1].grid(row=0, column=3, columnspan=1, padx=5, pady=5, sticky="ns")
# set initial entry state
if var_c.get():
widget_e[-1]['state'] = 'normal'
else:
widget_e[-1]['state'] = 'disabled'
# checkbox - binding
widget_c.bind("<ButtonRelease>", lambda event: set_entry_status(event, var_c, widget_e))
root = tk.Tk()
root.title('My Window')
CustomWidget(root, 'name', 'unit')
root.mainloop()
Indeed you can use command kwarg like I suggested in my other answer. In that case event must be removed from the arguments of your callback function:
import tkinter as tk
def set_entry_status(var, widg):
print(var.get())
if var.get():
widg[-1]['state'] = 'normal'
else:
widg[-1]['state'] = 'disabled'
def CustomWidget(frame, name, unit, ):
var_e = []
widget_e = []
var_c = tk.IntVar(master=frame, value=0)
widget_c = tk.Checkbutton(master=frame, text='', variable=var_c, command=lambda: set_entry_status(var_c, widget_e))
widget_c.grid(row=0, column=0, columnspan=1, padx=5, pady=5, sticky="ns")
label_l = name + " (" + unit + ")" # nome + unità di misura in parentesi per GUI
widget_l = tk.Label(frame, text=label_l, padx=1, pady=1)
widget_l.grid(row=0, column=1, columnspan=1, padx=5, pady=5, sticky="wns")
var_e.append(tk.StringVar(master=frame, value='A'))
widget_e.append(tk.Entry(frame, textvariable=var_e[-1], width=10, state="normal"))
widget_e[-1].grid(row=0, column=2, columnspan=1, padx=5, pady=5, sticky="ns")
var_e.append(tk.StringVar(master=frame, value='B'))
widget_e.append(tk.Entry(frame, textvariable=var_e[-1], width=10, state="normal"))
widget_e[-1].grid(row=0, column=3, columnspan=1, padx=5, pady=5, sticky="ns")
# set initial entry state
if var_c.get():
widget_e[-1]['state'] = 'normal'
else:
widget_e[-1]['state'] = 'disabled'
root = tk.Tk()
root.title('My Window')
CustomWidget(root, 'name', 'unit')
root.mainloop()
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)
I've created a desktop application using Tkinter in python3. There is a main class object 'Stack' for the application and within it is a function to render multiple frames for navigation to other windows within the app on a button click event.
I'm trying to display an image on the 'HomePage' screen (and actually all pages as a heading) using PIL.ImageTk/PIL.Image. Every time I run the app from terminal (macOS), the desktop app runs but the image does not appear. What am I doing wrong? Here is my code:
import getpass
import tkinter as tk
from tkinter import ttk
from PIL import ImageTk, Image
LARGE_FONT = ('Source Code Pro', 24)
class Stack(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
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 (HomePage, NdA):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky='news')
self.show_frame(HomePage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class HomePage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
username = getpass.getuser()
logo_path = 'myfilepath'.format(username)
img = ImageTk.PhotoImage(Image.open(logo_path+'myimage.png').resize((120,120)))
l = tk.Label(self, image=img)
l.grid(column=0, row=0, ipadx=10, ipady=5, sticky='w')
ttk.Label(
self,
text='Some Text',
wraplength=450,
justify=tk.LEFT
).grid(
column=1,
row=0,
ipady=10,
padx=(0,10),
sticky='nw')
tier1 = tk.Button(self, text='Button1', state=tk.DISABLED).grid(column=0, row=3, padx=10, columnspan=2, sticky='nesw')
tier2 = tk.Button(self, text='Button2', state=tk.DISABLED).grid(column=0, row=4, padx=10, columnspan=4, sticky='nesw')
tier3 = ttk.Button(self, text='Button3', command=lambda: controller.show_frame(Tier3))
tier3.grid(column=0, row=5, padx=10, columnspan=4, sticky='nesw')
nda = ttk.Button(self, text='Button4', command=lambda: controller.show_frame(NdA))
nda.grid(column=0, row=6, padx=10, pady=(0,10), columnspan=4, sticky='nesw')
class NdA(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
def clear_form():
effective_date.delete(0, END)
client_name.delete(0, END)
address.delete(0, END)
user_initials.delete(0, END)
def submit_data():
functions.create_nda(
date=effective_date.get(),
client_name=client_name.get(),
address=address.get(),
user=user_initials.get()
)
effective_date.delete(0, tk.END)
client_name.delete(0, tk.END)
address.delete(0, tk.END)
user_initials.delete(0, tk.END)
messagebox.showinfo('Heading Text', 'Message Text')
tk.Label(
self,
text='Some Text',
wraplength=450,
justify=tk.LEFT
).grid(
column=1,
row=0,
columnspan=3,
ipady=10,
padx=(0,10),
sticky='nw')
tk.Label(self, text='Label1').grid(column=0, row=2, pady=(20,10), columnspan=4, sticky='news')
tk.Label(self, text='Data1').grid(column=0, row=3, padx=(10,0), sticky='w')
client_name = tk.Entry(self, width=30)
client_name.grid(column=1, row=3, columnspan=3, padx=(0,10), sticky='news')
tk.Label(self, text='Data2').grid(column=0, row=5, padx=(10,0), sticky='w')
address = tk.Entry(self, width=30)
address.grid(column=1, row=5, columnspan=3, padx=(0,10), sticky='news')
tk.Label(self, text='Data3').grid(column=0, row=6, padx=(10,0), sticky='w')
effective_date = tk.Entry(self, width=30)
effective_date.insert(1, ' mm/dd/yyyy')
effective_date.grid(column=1, row=6, columnspan=3, padx=(0,10), sticky='news')
tk.Label(self, text='Data4').grid(column=0, row=7, padx=(10,0), sticky='w')
user_initials = tk.Entry(self, width=30)
user_initials.grid(column=1, row=7, columnspan=3, padx=(0,10), sticky='news')
create_tier3 = tk.Button(self, text='or click me', command=submit_data).grid(column=3, row=8, pady=(30,10), padx=(0,10), sticky='news')
clear_form = tk.Button(self, text='click me', command=clear_form).grid(column=2, row=8, pady=(30,10), padx=(5,5), sticky='news')
# return_home = tk.Button(self, text='HOME').grid(column=1, row=8, pady=(30,10), sticky='news')
app = Stack()
app.mainloop()
When the __init__() function in class HomePage exits the name img is garbage collected and so the label can't remember it. You need to save a reference to the image. The usual way is to save it as an attribute to the label:
l = tk.Label(self, image=img)
l.image = img # Save reference to image
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
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()