Related
All was going well as seen in the 1st pic below. all the cells are the same perfect size. its great.
But then comes the implementation of the textbox. and all hell breaks loose. as seen in the 2nd picture it completely disrupts my grid layout. i dont want the textbox adjusting cell sizes, i want it to go where i tell it to go like all the other widgets do. Ive spent hours on this and no avail!
import tkinter as tk
from tkinter import ttk, scrolledtext
root = tk.Tk()
root.state('zoomed')
root.configure(background='#8585ad')
for i in range(0,20):
for x in range(0,20):
root.columnconfigure(i, weight=1)
root.rowconfigure(x, weight=1)
for i in range(0, 20): # 0-19(20 is excluded) so this will loop 10x
for x in range(0, 20):
tk.Label(root, text=f"C-{i}, R-{x}", bg="green", fg="white").grid(column=i, row=x, sticky="NSEW", padx=1, pady=1)
main_frame = tk.Label(root, text="MAIN FRAME", bg="blue", fg="white", anchor="n").grid(column=1, row=1, columnspan=18, rowspan=18, sticky="NSEW")
frame1 = tk.Label(root, text="FRAME 1", bg="red", fg="white", anchor="n").grid(column=2, row=2, columnspan=3, rowspan=16, sticky="NSEW")
frame2 = tk.Label(root, text="FRAME 2", bg="green", fg="white", anchor="n").grid(column=6, row=2, columnspan=6, rowspan=16, sticky="NSEW")
frame3 = tk.Label(root, text=" FRAME 3", bg="red", fg="white", anchor="n").grid(column=13, row=2, columnspan=5, rowspan=16, sticky="NSEW")
for i in range(2, 5): # start at 2 and end after the 3rd loop.
for x in range(3, 18): # to loop 15x and for index to start at 3 so i then put (3,18), 18-3 = 15
tk.Label(root, text=f"Button-{(x-2)}", bg="white", fg="black").grid(column=i, row=x, sticky="EW", padx=5, pady=5)
frame1_header = tk.Label(root, text="User Panel", bg="black", fg="white").grid(column=2, row=2, columnspan=3, sticky="SEW", padx=5, pady=5)
frame2_header = tk.Label(root, text="Editor", bg="black", fg="white").grid(column=6, row=2, columnspan=6, sticky="SEW", padx=5, pady=5)
frame3_header = tk.Label(root, text="Info Panel", bg="black", fg="white").grid(column=13, row=2, columnspan=5, sticky="SEW", padx=5, pady=5)
frame2_text_area = tk.Label(root, text="Text Box", bg="black", fg="white", anchor="center").grid(column=6, row=3, columnspan=4, rowspan=15, sticky="NSEW", padx=5, pady=5)
frame2_list_box = tk.Label(root, text="List Box", bg="grey", fg="white", anchor="center").grid(column=10, row=3, columnspan=2, rowspan=15, sticky="NSEW", padx=5, pady=5)
frame3_tab_panel = ttk.Notebook(root)
frame3_tab_panel.grid(column=13, row=3, columnspan=5, rowspan=15, sticky="NSEW", padx=5, pady=5)
tab1 = ttk.Frame(root)
tab2 = ttk.Frame(root)
tab3 = ttk.Frame(root)
frame3_tab_panel.add(tab1, text ='Generic Editor')
frame3_tab_panel.add(tab2, text ='Text Compare')
frame3_tab_panel.add(tab3, text ='Script Ref')
# width and height does indeed adjust the texbox size but the textbox still isnt properly sticking to the grid that i set.
frame3_tab_panel_tab1 = tk.Text(root, relief="ridge", bd=2, undo=True, wrap="none", background='#1E1E1E', insertbackground='white')#, width=40, height=10)
frame3_tab_panel_tab1.grid(column=13, row=4, columnspan=5, rowspan=14, padx=5, pady=5)
frame3_tab_panel_tab1.config(font=('Consolas bold',10), fg="white")
frame3_tab_panel_tab1.focus()
root.mainloop()
"""
text_area = scrolledtext.ScrolledText(tab1, wrap = tk.WORD, width=40, height=10, font=("Times New Roman", 15))
text_area.grid(column = 0, pady = 10, padx = 10)
text_area.focus()
"""
without textbox. as you can see its all perfectly even.
FYI: this is just a template im working on so i can better understand tk's positioning.
textbox ruining grid by not adjusting itself accordingly and fitting to the grid i set.
There is a lot of wrong doing in your code and you really should take a good tutorial for tkinter and you may wish to have a brief overview of tkinters geometry management.
The biggest issue is whats causes your code to work differently as you expect it, you always define the root as the master. Every widget, except for the root window, has a master and is set by the ONLY positional argument every widget requiers. Note that if None is given, the root window is set by default. This is, because tkinter is built hirachically and at the top of this hirachy stands the root window (the instance of tk.Tk()).
A master should be a container and this means either the root window, a Toplevel or a Frame. Masters can have so called children, which can be every other widget plus frames that are handled as children. The relationship between a master and a frame are various, but for the scope of this question we will just look at the geometry.
Every widget has a geometry and can be received by the universal widget method .winfo_geometry() that will give you a geometry string 'widthxheight±x_offset±y_offset' (e.g. '120x50-0+20'). The geometry string is the basement for every calculations to order your widgets, which you can affect by choosing a geometry manager and different optional keywords. With those information an output will be created and displayed on your screen.
Suggestion:
import tkinter as tk
from tkinter import ttk, scrolledtext
def populate_frame_1():
frame_1_label = tk.Label(frame_1,text='User Panel',
background = 'black',
foreground = 'white')
frame_1_label.grid(column=0,row=0,sticky='ew',columnspan=3)
frame_1.columnconfigure(0,weight=1)
frame_1.columnconfigure(1,weight=1)
frame_1.columnconfigure(2,weight=1)
for i in range(0, 3):
for x in range(1, 16):
l = tk.Button(frame_1, text=f"Button-{(x-2)}",
bg="white", fg="black")
l.grid(column=i, row=x, sticky="EW", padx=5, pady=5)
def populate_frame_2():
frame_2_label = tk.Label(frame_2,text='Editor',
background = 'black',
foreground = 'white')
textbox = tk.Text(frame_2,width=35)
listbox = tk.Listbox(frame_2,bg='yellow')
frame_2_label.grid(column=0,row=0,sticky='ew',columnspan=6)
textbox.grid(column=0,row=1,sticky='ns',columnspan=4)
listbox.grid(column=4,row=1,sticky='ns',columnspan=2)
frame_2.rowconfigure(1,weight=2)
def populate_frame_3():
frame_3_label = tk.Label(frame_3,text='Info Panel',
background = 'black',
foreground = 'white')
frame_3_label.grid(column=0,row=0,sticky='ew',columnspan=5)
control_panel = ttk.Notebook(frame_3)
tab1 = ttk.Frame(control_panel)
tab2 = ttk.Frame(control_panel)
tab3 = ttk.Frame(control_panel)
control_panel.add(tab1, text ='Generic Editor')
control_panel.add(tab2, text ='Text Compare')
control_panel.add(tab3, text ='Script Ref')
control_panel.grid(column=0,row=1,sticky='nswe')
frame3_tab_panel_tab1 = tk.Text(tab1, relief="ridge", bd=2, undo=True,
wrap="none", background='#1E1E1E',
insertbackground='white',width=40, height=10)
frame3_tab_panel_tab1.pack(fill=tk.BOTH,expand=True)
frame3_tab_panel_tab1.config(font=('Consolas bold',10), fg="white")
frame3_tab_panel_tab1.focus()
frame_3.rowconfigure(1,weight=2)
frame_3.columnconfigure(0,weight=2)
XOFFSET = 75
YOFFSET = 50
root = tk.Tk()
root.state('zoomed')
root.configure(background='#8585ad')
main_frame = tk.Frame(root,background='blue')
frame_1 = tk.Frame(main_frame,background='red')
frame_2 = tk.Frame(main_frame,background='green')
frame_3 = tk.Frame(main_frame,background='red')
main_frame.pack(fill=tk.BOTH,expand=True,
padx=XOFFSET,pady=YOFFSET)
frame_1.pack(side=tk.LEFT,fill=tk.BOTH,padx=XOFFSET,pady=YOFFSET,expand=True)
frame_2.pack(side=tk.LEFT,fill=tk.Y,pady=YOFFSET,expand=True)
frame_3.pack(side=tk.LEFT,fill=tk.BOTH,padx=XOFFSET,pady=YOFFSET,expand=True)
populate_frame_1()
populate_frame_2()
populate_frame_3()
root.mainloop()
Change
frame3_tab_panel_tab1.grid(
column=13, row=4, columnspan=5, rowspan=14, padx=5, pady=5
)
to
frame3_tab_panel_tab1.grid(
column=13, row=4, columnspan=5, rowspan=14, padx=5, pady=5,
sticky="NSEW"
)
I managed to solve it by replacing the Text() widget with the scrolledtext.ScrolledText() widget. Its strange. No grid was required and if i remove height and width then it messes it up. Why does height and width have such an impact? why does it even exist when you have things like column and row configure along with sticky. Tkinter is quite confusing sometimes with its logic. But anyways, got there in the end.
Here's the code in case anyone encounters a similar issue.
import tkinter as tk
from tkinter import ttk, scrolledtext
root = tk.Tk()
root.state('zoomed')
root.configure(background='#8585ad')
for i in range(0,20):
for x in range(0,20):
root.columnconfigure(i, weight=1)
root.rowconfigure(x, weight=1)
for i in range(0, 20): # 0-19(20 is excluded) so this will loop 10x
for x in range(0, 20):
tk.Label(root, text=f"C-{i}, R-{x}", bg="green", fg="white").grid(column=i, row=x, sticky="NSEW", padx=1, pady=1)
main_frame = tk.Label(root, text="MAIN FRAME", bg="blue", fg="white", anchor="n").grid(column=1, row=1, columnspan=18, rowspan=18, sticky="NSEW")
frame1 = tk.Label(root, text="FRAME 1", bg="red", fg="white", anchor="n").grid(column=2, row=2, columnspan=3, rowspan=16, sticky="NSEW")
frame2 = tk.Label(root, text="FRAME 2", bg="green", fg="white", anchor="n").grid(column=6, row=2, columnspan=6, rowspan=16, sticky="NSEW")
frame3 = tk.Label(root, text=" FRAME 3", bg="red", fg="white", anchor="n").grid(column=13, row=2, columnspan=5, rowspan=16, sticky="NSEW")
for i in range(2, 5): # start at 2 and end after the 3rd loop.
for x in range(3, 18): # to loop 15x and for index to start at 3 so i then put (3,18), 18-3 = 15
tk.Label(root, text=f"Button-{(x-2)}", bg="white", fg="black").grid(column=i, row=x, sticky="EW", padx=5, pady=5)
frame1_header = tk.Label(root, text="User Panel", bg="black", fg="white").grid(column=2, row=2, columnspan=3, sticky="SEW", padx=5, pady=5)
frame2_header = tk.Label(root, text="Editor", bg="black", fg="white").grid(column=6, row=2, columnspan=6, sticky="SEW", padx=5, pady=5)
frame3_header = tk.Label(root, text="Info Panel", bg="black", fg="white").grid(column=13, row=2, columnspan=5, sticky="SEW", padx=5, pady=5)
frame2_text_area = tk.Label(root, text="Text Box", bg="black", fg="white", anchor="center").grid(column=6, row=3, columnspan=4, rowspan=15, sticky="NSEW", padx=5, pady=5)
frame2_list_box = tk.Label(root, text="List Box", bg="grey", fg="white", anchor="center").grid(column=10, row=3, columnspan=2, rowspan=15, sticky="NSEW", padx=5, pady=5)
frame3_tab_panel = ttk.Notebook(root)
frame3_tab_panel.grid(column=13, row=3, columnspan=5, rowspan=15, sticky="NSEW", padx=5, pady=5)
frame3_tab_panel_tab1 = scrolledtext.ScrolledText(root, bd=2, undo=True, wrap="none", width=40, height=10, font=("Times New Roman", 15), background='#1E1E1E', insertbackground='white')
frame3_tab_panel_tab1.config(font=('Consolas bold',10), fg="white")
frame3_tab_panel_tab1.focus()
tab2 = ttk.Frame(root)
tab3 = ttk.Frame(root)
frame3_tab_panel.add(frame3_tab_panel_tab1, text ='Generic Editor')
frame3_tab_panel.add(tab2, text ='Text Compare')
frame3_tab_panel.add(tab3, text ='Script Ref')
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 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)
...
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
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()