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 have deleted my previous posts as unusable for OS and would like to start over. As per Kate I have added code that I have tried along with the results. As per Bryan I have shortened script down to a minimal of 1 Checkbox and 1 mainframe and 3 additional frames. Any help would be greatly appreciated. Attached is the code I have used.
from tkinter import *
from tkinter import Tk
import tkinter as tk
from tkinter import ttk
from tkinter import Frame
root=Tk()
root.title("Dental Milling Machines")
root.geometry("400x200")
##Option 1 PRODUSES LINE 14 OBJECT IS NOT CALLABLE #####
#class HideFrames():
#def __init__(Frame):
#self.Frame = Frame()
#def HideFrames_info():
#def Hide():
#if cl is 0():
#Frame3.Hidden = True
#if c1 is 1():
#Frame3.Hidden = False
## Option 2 PRODUCES NO ERROR CODES But the code but will not work #####
class HideFrames():
def __call__(self):
self.Frame = Frame()
def HideFrames_info():
def Hide():
if cl is 0():
Frame3.Hidden = True
if c1 is 1():
Frame3.Hidden = False
#########################################
cb_var1 = tk.IntVar()
frame1 = Frame(root, height = 150, width= 150, relief= RAISED, bd=8, bg="blue")
frame2 = Frame(frame1, height = 150, width= 150, relief= RAISED, bd=8, bg="lightblue")
frame3 = Frame(frame1, height = 150, width= 150, relief= RAISED, bd=8, bg="lightblue")
frame4 = Frame(frame1, height = 150, width= 150, relief= RAISED, bd=8, bg="lightblue")
label = Label(frame3, text="FRAME 3", fg="red")
label.grid(row=0, columnspan=3, pady= 1, sticky= "W")
label = Label(frame4, text="FRAME 4", fg="red")
label.grid(row=0, columnspan=3, pady= 1, sticky= "W")
frame1.grid(row=0, column=0, pady=2,sticky="NW")
frame2.grid(row=1, column=0, pady=2,sticky="NW")
frame3.grid(row=2, column=0, pady=2,sticky="NW")
frame4.grid(row=3, column=0, pady=2,sticky="NW")
c1 = Checkbutton(frame2, text = "Check to SHOW Frame 3 Uncheck to HIDE Frame 3", variable=cb_var1)
c1.grid(row=1, column=0, pady= 1, padx= 5, sticky= "W")
app = HideFrames()
root.mainloop()
Good start. 2 big issues: You need to use the get() method to check the value of a tkinter variable, and you need to make the function run when checkbutton is triggered using the command argument. Try this:
import tkinter as tk
def frame3_disp():
if cb_var1.get():
frame3.grid(row=2, column=0, pady=2,sticky="NW")
else:
frame3.grid_forget()
root = tk.Tk()
root.title("Dental Milling Machines")
root.geometry("400x200")
frame1 = tk.Frame(root, height = 150, width= 150, relief= tk.RAISED, bd=8, bg="blue")
frame2 = tk.Frame(frame1, height = 150, width= 150, relief= tk.RAISED, bd=8, bg="lightblue")
frame3 = tk.Frame(frame1, height = 150, width= 150, relief= tk.RAISED, bd=8, bg="lightblue")
frame4 = tk.Frame(frame1, height = 150, width= 150, relief= tk.RAISED, bd=8, bg="lightblue")
label = tk.Label(frame3, text="FRAME 3", fg="red")
label.grid(row=0, columnspan=3, pady= 1, sticky= "W")
label = tk.Label(frame4, text="FRAME 4", fg="red")
label.grid(row=0, columnspan=3, pady= 1, sticky= "W")
frame1.grid(row=0, column=0, pady=2,sticky="NW")
frame2.grid(row=1, column=0, pady=2,sticky="NW")
frame4.grid(row=3, column=0, pady=2,sticky="NW")
cb_var1 = tk.IntVar()
c1 = tk.Checkbutton(frame2, text = "Check to SHOW Frame 3 Uncheck to HIDE Frame 3", variable=cb_var1, command=frame3_disp)
c1.grid(row=1, column=0, pady= 1, padx= 5, sticky= "W")
root.mainloop()
I am trying to call a function on a button click which will call second function that updates the fill color of an oval. I am getting an error that the object doesnt have the attribute. The goal of the program will be to eventually update the the signal light colors as variables linked to each go from 0 to 1 but right now i'm just getting the back end working. I have looked at dozens of examples over the last two days and cant get this to work.
import tkinter as tk
class WaysideSimulator(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
# self.pack()
self.grid()
self.create_widgets()
def create_widgets(self):
# Draw Buttons
self.Start_Simulation_Button = tk.Button(self, text="Start Simulation", command=self.start_simulation)
self.Start_Simulation_Button.grid(row=0, column=0)
self.End_Simulation_Button = tk.Button(self, text="End Simulation", fg="red", command=self.master.destroy)
self.End_Simulation_Button.grid(row=1, column=0)
# Top Signal Buttons
self.Top_Red_Button = tk.Button(self, text="Set Top Signal Red", command=self.set_top_red)
self.Top_Red_Button.grid(row=2, column=0)
self.Top_Yellow_Button = tk.Button(self, text="Set Top Signal Yellow", command=self.set_top_yellow)
# self.Searchlight_Button.pack(side="bottom")
self.Top_Yellow_Button.grid(row=3, column=0)
self.Top_Green_Button = tk.Button(self, text="Set Top Signal Green", command=self.set_top_green)
self.Top_Green_Button.grid(row=4, column=0)
self.Top_Black_Button = tk.Button(self, text="Set Top Signal Black", command=self.set_top_off)
self.Top_Black_Button.grid(row=5, column=0)
# Middle Signal Buttons
self.Middle_Red_Button = tk.Button(self, text="Set Middle Signal Red", command=self.set_middle_red)
self.Middle_Red_Button.grid(row=2, column=1)
self.Middle_Yellow_Button = tk.Button(self, text="Set Middle Signal Yellow", command=self.set_middle_yellow)
self.Middle_Yellow_Button.grid(row=3, column=1)
self.Middle_Green_Button = tk.Button(self, text="Set Middle Signal Green", command=self.set_middle_green)
self.Middle_Green_Button.grid(row=4, column=1)
self.Middle_Black_Button = tk.Button(self, text="Set Middle Signal Black", command=self.set_middle_black)
self.Middle_Black_Button.grid(row=5, column=1)
# Bottom Signal Buttons
self.Bottom_Red_Button = tk.Button(self, text="Set Bottom Signal Red", command=self.set_bottom_red)
self.Bottom_Red_Button.grid(row=2, column=2)
self.Bottom_Yellow_Button = tk.Button(self, text="Set Bottom Signal Yellow", command=self.set_bottom_yellow)
self.Bottom_Yellow_Button.grid(row=3, column=2)
self.Bottom_Green_Button = tk.Button(self, text="Set Bottom Signal Green", command=self.set_bottom_green)
self.Bottom_Green_Button.grid(row=4, column=2)
self.Bottom_Black_Button = tk.Button(self, text="Set Bottom Signal Black", command=self.set_bottom_black)
self.Bottom_Black_Button.grid(row=5, column=2)
# Draw the Canvas
self.canvas = tk.Canvas(self, width=500, height=500, bg="white")
self.canvas.grid()
# Draw the Searchlight Signal Heads
self.signal_pole = self.canvas.create_rectangle(245, 20, 255, 500, fill="grey")
self.top_signal_head = self.canvas.create_oval(200, 10, 300, 110, fill="black")
self.middle_signal_head = self.canvas.create_oval(200, 160, 300, 260, fill="black")
self.bottom_signal_head = self.canvas.create_oval(200, 310, 300, 410, fill="black")
self.top_signal_light = self.canvas.create_oval(240, 50, 260, 70, fill="red", tags="top_light")
self.top_signal_light_ON = True
self.middle_signal_light_ON = self.canvas.create_oval(240, 200, 260, 220, fill="yellow", tags="middle_light")
self.middle_light_ON = True
self.bottom_signal_light = self.canvas.create_oval(240, 350, 260, 370, fill="green", tags="bottom_light")
self.bottom_signal_light_ON = True
# Function Definitions
def set_top_red(self):
self.canvas.itemconfig("top_light", fill="Red")
self.top_signal_light_ON = True
print("Top signal is RED")
def set_top_yellow(self):
self.canvas.itemconfig("top_light", fill="yellow")
self.top_signal_light_ON = True
print("Top signal is YELLOW")
def set_top_green(self):
self.canvas.itemconfig("top_light", fill="green")
self.top_signal_light_ON = True
print("Top signal is GREEN.")
def set_top_off(self):
self.canvas.itemconfig("top_light", fill="black")
self.top_signal_light_ON = False
print("Top signal is OFF.")
def set_middle_red(self):
self.canvas.itemconfig("middle_light", fill="Red")
self.middle_light_ON = True
print("Middle signal is RED")
def set_middle_yellow(self):
self.canvas.itemconfig("middle_light", fill="yellow")
self.middle_light_ON = True
print("middle signal is YELLOW")
def set_middle_green(self):
self.canvas.itemconfig("middle_light", fill="green")
self.middle_light_ON = True
print("Middle signal is GREEN.")
def set_middle_black(self):
self.canvas.itemconfig("middle_light", fill="black")
self.middle_light_ON = False
print("Middle signal is OFF.")
def set_bottom_red(self):
self.canvas.itemconfig("bottom_light", fill="Red")
self.middle_light_ON = True
print("Bottom signal is RED")
def set_bottom_yellow(self):
self.canvas.itemconfig("bottom_light", fill="yellow")
self.middle_light_ON = True
print("Bottom signal is YELLOW")
def set_bottom_green(self):
self.canvas.itemconfig("bottom_light", fill="green")
self.middle_light_ON = True
print("Bottom signal is GREEN.")
def set_bottom_black(self):
self.canvas.itemconfig("bottom_light", fill="black")
self.middle_light_ON = False
print("Bottom signal is OFF.")
# Simulation Functions
def start_simulation(self):
print("Searchlight Simulation Started")
self.canvas.itemconfig("top_light", fill="black")
self.canvas.itemconfig("middle_light", fill="black")
self.canvas.itemconfig("bottom_light", fill="black")
self.top_signal_light_ON = False
self.middle_signal_light_ON = False
self.bottom_signal_light_ON = False
print("All signals are OFF")
def main():
root = tk.Tk()
root.title("Searchlight Simulator")
root.geometry("500x650")
# root.resizable(0, 0)
s1 = WaysideSimulator(master=root)
s1.mainloop()
if __name__ == '__main__':
main()
I have a GUI where I have created a canvas and I have two frames created inside it and using canvas.create_window.One on the top with four push buttons.On click of a button I should create a frame with some widgets down to the first frame. So, now I want previous frame to be detroyed(or refreshed or cleared or deleted) while i am switching between the buttons.Now the frames are getting stacked on top of each other.so below is my code,
from tkinter import *
#Creating root window
root = Tk()
root.geometry("1000x1000")
#Still don't know how it works but some how made it work :P :P
root.grid_rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=0)
#Creaed canvas for setting up a scroll bar
canv=Canvas(root,width=850, height=650, background='blue')
vsb = Scrollbar(root, orient="vertical", command=canv.yview)
canv.config(yscrollcommand=vsb.set)
vsb.grid(row=1,column=1,sticky="ns")
canv.config(scrollregion=(0, 0,600,1000))
canv.grid(row=1, column=0)
canv.bind('<Configure>',canv.config(scrollregion=canv.bbox('all')))
#Frame for the top push buttons
mainframe1 = Frame(root, height = 20,width= 250,bg = 'white')
canvas_frame = canv.create_window((4, 4),window=mainframe1,anchor="nw")
b1 = Button(mainframe1,text = "butt 1",width = 24, height = 1,command=lambda: buttonClick(1))
b2 = Button(mainframe1,text = "butt 2",width = 24, height = 1,command=lambda: buttonClick(2))
b3 = Button(mainframe1,text = "butt 3",width = 24, height = 1,command=lambda: buttonClick(3))
b4 = Button(mainframe1,text = "Butt 4",width = 24, height = 1,command=lambda: buttonClick(4))
b1.grid(row=0, column=0, padx=15, pady=10)
b2.grid(row=0, column=1, padx=15, pady=10)
b3.grid(row=0, column=2, padx=15, pady=10)
b4.grid(row=0, column=3, padx=15, pady=10)
#Method to work based on Button click
def buttonClick(num):
if num == 1:
#Creating Frame inside Canvas
mainframe2 = Frame(root, height = 200,width= 250,bg = 'white')
canvas_frame = canv.create_window((80,100),window = mainframe2,anchor="nw")
label = Label(mainframe2,text="frame for butt 1",font=('Courier',-20,'bold'))
#label.grid_rowconfigure(1,weight=1)
#label.columnconfigure(1,weight=1)
label.grid(row=0,column=3)
#Create a Tkinter variable
varmonth = StringVar()
varyear = StringVar()
varmonth.set('January') # set the default option
varyear.set('2017')
#Option Menu for Dropdown list boxes
#For Month
popupMenu = OptionMenu(mainframe2, varmonth, 'January', 'February', 'March', 'April', 'May','June','July','August','Septempber','October','November','December')
Label(mainframe2, text="Select Month",font=('Courier',-15,'bold')).grid(row=2, column=1,sticky='e',padx=15,pady=15)
popupMenu.grid(row=2, column=2,padx=20,sticky="ew")
#For year
popupMenu = OptionMenu(mainframe2, varyear, '2017','2018','2019')
Label(mainframe2, text="Select Year",font=('Courier',-15,'bold')).grid(row=2, column=7,padx=20,pady=25)
popupMenu.grid(row=2, column=8,padx=15,pady=20)
#For Generate Report Button
buttongreport = Button(mainframe2,text = 'Generate Sales report',width = 24, height = 1,command=lambda: buttonClick(gr))
buttongreport.grid(row=8, column=3,columnspan=2, padx=10, pady=10)
elif(num==2):
mainframe3 = Frame(root, height=200, width=250, bg='white')
canvas_frame = canv.create_window((80, 100), window=mainframe3, anchor="nw")
label = Label(mainframe3, text="Frame for butt 2", font=('Courier', -20, 'bold'))
# label.grid_rowconfigure(1,weight=1)
# label.columnconfigure(1,weight=1)
label.grid(row=0, column=3)
# Create a Tkinter variable
varmonth = StringVar()
varyear = StringVar()
varmonth.set('January') # set the default option
varyear.set('2017')
popupMenu = OptionMenu(mainframe3, varmonth, 'January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'Septempber', 'October', 'November', 'December')
Label(mainframe3, text="Select Month", font=('Courier', -15, 'bold')).grid(row=2, column=1, sticky='e', padx=15,
pady=15)
popupMenu.grid(row=2, column=2, padx=20, sticky="ew")
popupMenu = OptionMenu(mainframe3, varyear, '2017', '2018', '2019')
Label(mainframe3, text="Select Year", font=('Courier', -15, 'bold')).grid(row=2, column=7, padx=20, pady=25)
popupMenu.grid(row=2, column=8, padx=15, pady=20)
buttongreport = Button(mainframe3, text='Generate Sales report', width=24, height=1,
command=lambda: buttonClick(gr))
buttongreport.grid(row=8, column=3, columnspan=2, padx=10, pady=10)
elif(num==3):
mainframe4 = Frame(root, height=200, width=250, bg='white')
canvas_frame = canv.create_window((80, 100), window=mainframe4, anchor="nw")
label = Label(mainframe4, text=" Frame for Butt 4", font=('Courier', -20, 'bold'))
# label.grid_rowconfigure(1,weight=1)
# label.columnconfigure(1,weight=1)
label.grid(row=0, column=2)
# Create a Tkinter variable
varmonth = StringVar()
varyear = StringVar()
varmonth.set('1: January to April') # set the default option
varyear.set('2017')
popupMenu = OptionMenu(mainframe4, varmonth, '1: January to April', '2: May to August', '3: Septempber to December')
Label(mainframe4, text="Select Quarter", font=('Courier', -15, 'bold')).grid(row=2, column=1, sticky='e', padx=15,
pady=15)
popupMenu.grid(row=2, column=2, padx=20, sticky="ew")
popupMenu = OptionMenu(mainframe4, varyear, '2017', '2018', '2019')
Label(mainframe4, text="Select Year", font=('Courier', -15, 'bold')).grid(row=2, column=7, padx=20, pady=25)
popupMenu.grid(row=2, column=8, padx=15, pady=20)
buttongreport = Button(mainframe4, text='Generate Sales report', width=24, height=1,
command=lambda: buttonClick(gr))
buttongreport.grid(row=8, column=2, padx=10, pady=10)
root.mainloop()
I have written the code in procedural way.Sorry for the long code.
You can use canvas.delete(...) to remove the currently displayed frame whenever a button is clicked. But you need to declare a global variable to hold the current frame displayed:
open_frame = None
Then modify buttonClick(...):
def buttonClick(num):
global open_frame
if open_frame:
canv.delete(open_frame)
if num == 1:
...
open_frame = canv.create_window(...)
...
elif num == 2:
...
open_frame = canv.create_window(...)
...
elif num == 3:
...
open_frame = canv.create_window(...)
...
You can use frame.destroy().
destroy() method will delete the frame and all of its children will also be destroyed.