How to use a statusbar in many frames? - python

I managed to build a little GUI, where I can switch between frames related to the following question: Switch between frames in tkinter
I wanted to have a status bar on the Bottom of my GUI and it should stay on every frame! The status bar shows info about buttons when hovering over it!
Not hovered:
Hovered:
As you can see it works, BUT I can't manage to make the status bar update its values when another frame is raised, because I could only manage to create the status bar in one frame class...
class SampleApp(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
self.title_font = font.Font(family='Helvetica', size=18,
weight="bold", slant="italic")
container = 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 (start_frame, cr_frame, db_frame):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.pack()
self.show_frame("start_frame")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
for frame in self.frames.values():
#frame.grid_remove()
frame.pack_forget()
frame = self.frames[page_name]
frame.pack()
if page_name == "start_frame":
frame.winfo_toplevel().geometry("545x200")
if page_name == "cr_frame":
frame.winfo_toplevel().geometry("600x200")
if page_name == "db_frame":
frame.winfo_toplevel().geometry("700x630")
class start_frame(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
self.controller = controller
self.btn_cr = Button(self, text="Copyright Analyse", command=lambda: self.controller.show_frame("cr_frame"), width=40)
self.btn_cr.pack(side=LEFT, padx=15, pady=1, ipady=40)
self.btn_db = Button(self, text="Copyright Datenbank", command=lambda: self.controller.show_frame("db_frame"), width=40)
self.btn_db.pack(side=LEFT, pady=1, ipady=40)
###################Here is the statusbar defined + bindings and so on##########
self.lbl_status = Label(self.controller, text="...", border=1, relief=SUNKEN, anchor=W)
self.lbl_status.pack(side=BOTTOM, fill=X, anchor=W)
self.btn_cr.bind("<Enter>", lambda event: self.lbl_status.configure(text="Open copyright analysis window..."))
self.btn_cr.bind("<Leave>", self.leave_bindings)
self.btn_db.bind("<Enter>", lambda event: self.lbl_status.configure(text="Open copyright database..."))
self.btn_db.bind("<Leave>", self.leave_bindings)
class cr_frame(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
self.controller = controller
Now, if I switch to page 2 (cr_frame), the status bar is still there because I attached it to the top-level controller (of all frames) but I can't edit it through the cr_frame class...
I don't know how to do it.

First, move the statusbar to the main app since it's part of the app and not part of a page:
class SampleApp(Tk):
def __init__(self, *args, **kwargs):
...
container = Frame(self)
self.lbl_status = Label(self, text="", border=1, relief=SUNKEN, anchor=W)
container.pack(side="top", fill="both", expand=True)
self.lbl_status.pack(side="bottom", fill="x")
Next, add a method to the app for setting the status:
class SampleApp(Tk):
...
def set_status(self, string):
self.lbl_status.configure(text=string)
Finally, call that method whenever you need to change the status:
class cr_frame(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
self.controller = controller
...
def something(self):
...
self.controller.set_status("Hello, world")
...

Related

How do I pass variables in different classes?

I am having trouble with passing the variables into different classes. I am a beginner. I am trying to get the entry box to pass the textvariable which is self.margaritapizza(). However, when I try run it, it says 'nextage' has no attribute to 'margaritapizza'. So not sure how to pass the variable to different classes. I am using tkinter and it is in visual studio code if that helps.
import tkinter as tk
from tkinter import IntVar
class SampleApp(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.margaritapizza = IntVar()
self.frames = {}
for F in (StartPage, nextpage):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
def show_frame(self, page_name):
frame = self.frames[page_name]
'''Show a frame for the given page name'''
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg='#3d3d5c')
self.controller = controller
heading_label = tk.Label(self,
text="start page",
font=('arial', 45, 'bold'),
foreground='#ffffff',
background='#3d3d5c')
heading_label.pack(pady=25)
def next():
controller.show_frame('nextpage')
next_button = tk.Button(text="start page",command=next).pack
class nextpage(tk.Frame,SampleApp):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg='#3d3d5c')
self.controller = controller
heading_label = tk.Label(self,
text="start page",
font=('arial', 45, 'bold'),
foreground='#ffffff',
background='#3d3d5c')
heading_label.pack(pady=25)
entry = tk.entry(borderwidth=2,width=15,textvariable=self.margaritapizza).pack
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
First, nextpage should definitely not inherit from SampleApp. So, change the definition to:
class nextpage(tk.Frame):
Second, margaritapizza is an attribute of SampleApp, so you need to refer to it via the instance of SampleApp. In your case, self.controller is that instance, so you can use self.controller.margaritapizza.
There are other problems with how you define the entry, I'll fix them as well:
self.entry = tk.Entry(self, borderwidth=2,width=15,textvariable=self.controller.margaritapizza)
self.entry.pack()

Updating tkinter label in a frame object

I have been working to improve my Tkinter skills through a combination of books, on-line tutorials, and utilizing questions and examples from this site. In one script I am working to understand and build upon this question and popular answer:
Switch between two frames in tkinter.
I have created a version that allows the creation of multiple instances of the pages, tracks the number of time page 1 has been selected for each instance, and updates the label for page 1 with the number of times page 1 has been selected. To reduce the size of the program the code shown is only for the updating of the page 1 label. I have only been able to accomplish this through the use of a global variable in the 'PageOne' object and the method that is called to show it. I feel
there must be a better way but all of the ways I tried yields an error similar
to : #AttributeError: '_tkinter.tkapp' object has no attribute 'label'. Should be simple I thought but I spent a couple of days on this issue alone.
import tkinter as tk
from tkinter import font as tkfont
'''
original post
https://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter
'''
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
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.Label1_Text = tk.StringVar()
self.PageOne_Ct = tk.IntVar()
#-------------------------------
self.frames = {}
for F in (StartPage, PageOne, PageTwo):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
def get_lbl_text(self):
label1text = self.Label1_Text.get()
return label1text
def get_page1_cts(self):
counts = self.PageOne_Ct.get()
return counts
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This is the start page", font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
button1 = tk.Button(self, text="Go to Page One",
command=lambda: PageOne.Show_PageOne(self,
parent, controller))
button2 = tk.Button(self, text="Go to Page Two",
command=lambda: controller.show_frame("PageTwo"))
button1.pack()
button2.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
global label
self.controller = controller
label = tk.Label(self, text = self.controller.Label1_Text.get(),
font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame("StartPage"))
button.pack()
def Show_PageOne(self, parent, controller):
global label
count = self.controller.get_page1_cts()
self.controller = controller
count = self.controller.PageOne_Ct.get()
count += 1
self.controller.PageOne_Ct.set(count)
new_label1_text = "You have clicked page one {} times".format(count)
self.controller.Label1_Text.set(new_label1_text)
label.configure(text=new_label1_text)
controller.show_frame("PageOne")
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label2 = tk.Label(self, text="This is page 2", font=controller.title_font)
label2.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame("StartPage"))
button.pack()
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
Here is an example that doesn't require passing a bunch of vars around.
import tkinter as tk
'''
PREVIOUS|NEXT DISPLAY FOR PAGES
'''
class Book:
def __init__(self, master, pages):
#init page vars
self.pnum = 0
self.pages = pages
self.page = self.pages[self.pnum].show()
#next/prev buttons
tk.Button(master, text='prev', command=self.prev).place(relx=.05, rely=.95, anchor='center')
tk.Button(master, text='next', command=self.next).place(relx=.95, rely=.95, anchor='center')
def next(self):
self.page.hide()
self.pnum = (self.pnum+1)%len(self.pages)
self.page = self.pages[self.pnum].show()
def prev(self):
self.page.hide()
self.pnum = range(len(self.pages))[self.pnum-1]
self.page = self.pages[self.pnum].show()
'''
GENERIC PAGE FOR ALL PAGES TO INHERIT FROM
'''
class Page(tk.Frame):
def __init__(self, master, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
self.visits = 0
self.label = tk.Label(self, text=f'visits: {self.visits}', font='consolas 16 bold', fg='white', bg=self['bg'])
self.label.grid()
def show(self) -> tk.Frame:
self.visits += 1
self.label.configure(text=f'visits: {self.visits}')
self.grid(row=0, column=0, sticky='nswe')
return self
def hide(self):
self.grid_forget()
'''
EXAMPLE PAGES
'''
class Page_1(Page):
def __init__(self, master, **kwargs):
Page.__init__(self, master, **kwargs)
class Page_2(Page):
def __init__(self, master, **kwargs):
Page.__init__(self, master, **kwargs)
class Page_3(Page):
def __init__(self, master, **kwargs):
Page.__init__(self, master, **kwargs)
'''
ROOT
'''
class Root(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title("Pages Example")
self.geometry('800x600+100+100')
#make the page span the entire display
self.grid_columnconfigure(0, weight=1)
self.grid_rowconfigure(0, weight=1)
#create book
Book(self, [Page_1(self, bg='blue') ,
Page_2(self, bg='red') ,
Page_3(self, bg='green'),
])
self.mainloop()
Root() if __name__ == "__main__" else None

Tkinter switching frame with conditions

I use following code:
Switch between two frames in tkinter
import tkinter as tk # python 3
from tkinter import font as tkfont # python 3
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
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 (StartPage, PageOne):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This is the start page", font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
self.giris = tk.Entry(self)
self.giris.pack()
self.yazi = tk.Label(self, text="Buraya GİRİLEN VERİ gelecek.")
self.yazi.pack()
button2 = tk.Button(self, text="ae",
command=lambda: [self.alinanmetin(), controller.show_frame("PageOne")])
button2.pack()
def alinanmetin(self):
il = self.giris.get()
self.yazi.config(text="Girdiğiniz il: %s" % il)
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This is page 1", font=controller.title_font)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame("StartPage"))
button.pack()
if __name__ == "__main__":
app = SampleApp()
app.geometry("600x400")
app.mainloop()
I want to change my frame according to the value I get from the entry.
if(il =="ali") i want change my frame and
if( il != "ali") i dont want do anything.
I am currently running two functions at the same time.How can i change frame after check value.

How to update listbox when I change view in Tkinter?

I need to update listbox when I'm changing view, but I dont know how to do it. On first page I'm adding some items to list and on second it should to show all items in listbox.
# -*- coding: utf-8 -*-
from tkinter import *
tb1 = [["Kofola", "0,5","30"]]
class SeaofBTCapp(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
container = 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 (StartPage, PageOne):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(Frame):
def __init__(self, parent, controller):
Frame.__init__(self,parent)
label = Label(self, text="Start Page")
label.pack(pady=10,padx=10)
button = Button(self, text="Visit Page 1",
command=lambda: controller.show_frame(PageOne))
button.pack()
button2 = Button(self, text="add",
command=self.add)
button2.pack()
def add(self):
tb1.append(["Radegast", "0,5","30"])
print(tb1)
class PageOne(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
label = Label(self, text="Page One!!!")
label.pack(pady=10,padx=10)
button1 = Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
self.bill=Listbox(self)
self.bill.pack()
for item in tb1:
co=" ".join(str(x) for x in item)
self.bill.insert(END, co)
app = SeaofBTCapp()
app.mainloop()
In the PageOne class You are reading the list tb1 only once, in __init__(). To get the changes in tb1 to be seen in the listbox you also have to update the listbox with the new altered list.
There is also an issue with the list. As it's defined in the global namespace your app will depend on this. I'd suggest you define it in the SeaofBTCapp() __init__() function and then you can access it through the controller object:
class SeaofBTCapp(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
self.tb1 = [["Kofola", "0,5","30"]]
... etc ...
class StartPage(Frame):
def __init__(self, parent, controller):
self.controller = controller
... etc ...
def add(self):
self.controller.tb1.append(["Radegast", "0,5","30"])
... etc ...
And then add an update() method to the PageOne() class which updates the listbox and calls it from the add() method. I'm calling by way of controller method update_pageone(). See full example below:
from tkinter import *
class SeaofBTCapp(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
self.tb1 = [["Kofola", "0,5","30"]] # Create instance variable tb1
container = 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 (StartPage, PageOne):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
def update_pageone(self):
self.frames[PageOne].update() # Call update on PageOne
class StartPage(Frame):
def __init__(self, parent, controller):
self.controller = controller # Remember the controller
Frame.__init__(self,parent)
label = Label(self, text="Start Page")
label.pack(pady=10,padx=10)
button = Button(self, text="Visit Page 1",
command=lambda: self.controller.show_frame(PageOne))
button.pack()
button2 = Button(self, text="add", command=self.add)
button2.pack()
def add(self):
self.controller.tb1.append(["Radegast", "0,5","30"])
self.controller.update_pageone() # Asking controller for an update
class PageOne(Frame):
def __init__(self, parent, controller):
self.controller = controller # Remember the controller
Frame.__init__(self, parent)
label = Label(self, text="Page One!!!")
label.pack(pady=10,padx=10)
button1 = Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
self.bill = Listbox(self)
self.bill.pack()
for item in controller.tb1:
co = " ".join(str(x) for x in item)
self.bill.insert(END, co)
def update(self):
# Delete all from Listbox bill
self.bill.delete(0, 'end')
# Add revised table into Listbox bill
for item in self.controller.tb1:
co = " ".join(str(x) for x in item)
self.bill.insert(END, co)
app = SeaofBTCapp()
app.mainloop()

python update tk.optionmenu drop down selections

I have a superclass (tk window) , and another subclass (tk window) which inherits properties from the original class. on the super class, I have a method callback which is called every second. The subclass displays a tk options menu, and i would like to update the options displayed in the tk options menu though the superclass method which is called every second. I've created a shortened version of my program which shows generally what I'm trying to do. This program will run, and it works as i'd like it to, but in pycharm I receive a warning highlighting the subclass method def update(self, controller)::
Signature of method 'OtherPage.update()' does not match
signature of base method in class 'Misc'
what is the correct way to do this?
import tkinter as tk
exchanges = ['Bitfinex', 'Bittrex', 'Kraken', 'Gdax']
class Trader(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Multi-Trader-Wallet")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
menubar = tk.Menu(container)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label='Exit', command=lambda: print('exit'))
menubar.add_cascade(label='File', menu=filemenu)
tk.Tk.config(self, menu=menubar)
self.exchange1 = tk.StringVar()
self.frames = {}
self.id = self.after(1000, self.callback)
for F in (MainPage, OtherPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, columnspan=1, rowspan=1, sticky="nsew")
self.show_frame(OtherPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
def callback(self):
print('callback')
print(self.frames[OtherPage])
self.frames[OtherPage].update(self)
self.id = self.after(1000, self.callback)
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text='MainPage')
label.grid(row=1, column=1)
class OtherPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label2 = tk.Label(self, text='OtherPage')
label2.grid(row=1, column=1)
drop = tk.OptionMenu(self, controller.exchange1, *exchanges)
drop.config(width=10)
drop.grid(row=2, column=1)
def update(self, controller):
exchanges.append('new exchange')
drop = tk.OptionMenu(self, controller.exchange1, *exchanges)
drop.config(width=10)
drop.grid(row=2, column=1)
app = Trader()
app.mainloop()

Categories

Resources