I can't get a navigation bar to show and hide itself. I'm using classes to seperate the code meant for each particular page.
I've tried the below code. I expected the navigation bar to show itself with buttons, that, when clicked - change the page.
I should be able to click on the nav button/menu button where it shows a drop down all pages available, allowing the user to then click on one of the buttons in the drop down to navigate to a page of their desire.
from tkinter import *
import tkinter as tk
from PIL import ImageTk,Image
def toggle_win(root):
w = root
f1 = Frame(w,width=300,height=600,bg='#12c4c0')
f1.place(x=0,y=0)
def bttn(x,y,text,bcolor,fcolor,cmd):
def on_entera(e):
MyButton1['background'] = bcolor
MyButton1['foreground'] = "#262626"
def on_leavea(e):
MyButton1['background'] = fcolor
MyButton1['foreground'] = "#262626"
MyButton1 = Button(
f1,
text=text,
width=42,
height=2,
fg="#262626",
border=0,
bg=fcolor,
activeforeground="#262626",
activebackground=bcolor,
command=cmd
)
MyButton1.bind("<Enter>", on_entera)
MyButton1.bind("<Leave>", on_leavea)
MyButton1.place(x=x,y=y)
# x,y, text, bcolor, fcolor, cmd
bttn(0,80,'Home','#0f9d9a','#12c4c0',None)
bttn(0,117,'Two','#0f9d9a','#12c4c0',None)
bttn(0,154,'Three','#0f9d9a','#12c4c0',None)
bttn(0,191,'Four','#0f9d9a','#12c4c0',None)
bttn(0,228,'Five','#0f9d9a','#12c4c0',None)
bttn(0,265,'Six','#0f9d9a','#12c4c0',None)
def dele():
f1.destroy()
global img2
img2=ImageTk.PhotoImage(Image.open('close.png'))
Button(f1,image=img2,command=dele,border=0,activebackground='#12c4c0',bg="#12c4c0").place(x=0,y=0)
class ScrollableFrame(tk.Frame):
def __init__(self, container, *args, **kwargs):
super().__init__(container, *args, **kwargs)
canvas = tk.Canvas(self)
scrollbar = tk.Scrollbar(self, orient="vertical", command=canvas.yview)
self.scrollable_frame = tk.Frame(canvas)
self.scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(
scrollregion=canvas.bbox("all")
)
)
canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)
canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
class Page(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
def show(self):
self.lift()
class Page1(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
# Create a frame for the page based on the ScrollableFrame class
frame = ScrollableFrame(self)
frame.pack(fill="both", expand=True)
# Create x number of labels for this page
for i in range(10):
tk.Label(frame.scrollable_frame, text="Label {}".format(i)).pack()
class Page2(Page):
def __init__(self, *args, **kwargs):
Page.__init__(self, *args, **kwargs)
# Create a frame for the page based on the ScrollableFrame class
frame = ScrollableFrame(self)
frame.pack(fill="both", expand=True)
# Create x number of labels for this page
for i in range(10):
tk.Label(frame.scrollable_frame, text="Label {}".format(i)).pack()
class MainView(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
p1 = Page1(self)
p2 = Page2(self)
buttonframe = tk.Frame(self)
container = tk.Frame(self)
buttonframe.pack(side="top", fill="x", expand=False)
container.pack(side="top", fill="both", expand=True)
p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
# b1 = tk.Button(buttonframe, text="Scheduling Tools", command=p1.show)
# b2 = tk.Button(buttonframe, text="Data Cleansing Tools", command=p2.show)
# #b1.pack(side="left")
# #b2.pack(side="left")
toggle_win(root=container)
img1=ImageTk.PhotoImage(Image.open('open.png'))
button = Button(buttonframe,command=lb.toggle_win(root=container),image=img1,border=0,bg="#262626",activebackground='#262626')
button.place(x=0,y=0)
p1.show()
def main():
root = tk.Tk()
main = MainView(root)
main.pack(side="top", fill="both", expand=True)
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
root.geometry(f"{screen_width}x{screen_height}")
root.title("dmin tools")
root.iconbitmap("python.ico")
root.mainloop()
if "__main__" == __name__:
main()