In my GUI, i wanted to display an image that changes depending on some value. The image would change between self.img1 and self.img2. I created separate classes for the container and the pages. The container is defined as such:
class Gui(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)
self.MyReading = StringVar()
self.redpic = Image.open("red.png")
self.redpic = self.redpic.resize((100,100), Image.ANTIALIAS)
self.greenpic = Image.open("green.png")
self.greenpic = self.greenpic.resize((100,100), Image.ANTIALIAS)
self.img1 = ImageTk.PhotoImage(self.redpic)
self.img2 = ImageTk.PhotoImage(self.greenpic)
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()
And the page displaying the image:
class StartPage(Frame):
def __init__(self, parent, controller):
Frame.__init__(self,parent)
label = Label(self, text="StartPage")
label.grid()
label1 = Label(self, textvariable = controller.MyReading)
label1.grid();
self.label4 = Label(self, image = controller.img1)
self.label4.grid();
self.label4.image = controller.img1
button1 = Button (self, text = "Show PageOne", command = lambda: controller.show_frame(PageOne))
button1.grid()
It is currently displaying img1. Now, to instantiate the GUI:
root = Gui()
update_reading()
root.mainloop()
update_reading() updates my other labels defined with StringVar(). I was wondering how would I go about updating label4 (which shows the image) if I can only instantiate/get access to Gui()? I only know that I could change the label4 through configure(). Is there a textvariable equivalent for images?
EDIT: I forgot to put the logic that I wanted to implement. It is basically:
If foo == TRUE:
--change the image to img1--
else:
--change the image to img2--
for some foo that exists outside of Gui.
EDIT2: Following through a previous comment's logic, I made some small changes to the code In the Gui:
class Gui(Tk):
def __init__(self, *args, **kwargs):
self.ColorVar = DoubleVar()
And within StartPage(), the changes are:
class StartPage(Frame):
def __init__(self, parent, controller):
controller.ColorVar.trace("w",self.IdkChief(controller))
def IdkChief(self, controller):
global val1
if float(val1) < 2.50 :
self.label4.configure(image = controller.img2)
self.label4.image = controller.img2
else:
self.label4.configure(image = controller.img1)
self.label4.image = controller.img1
Then the changes on ColorVar is defined in update_reading()as such:
def update_reading():
global val1
root.ColorVar.set(val1)
root.after(100,update_reading)
Where val1 is a changing float value. I decided to change it from a boolean logic to a float one to increase flexibility. It would then throw me a generic error
Exception in Tkinter callback Traceback (most recent call last):
File
"C:\Users\AppData\Local\Programs\Python\Python37\lib\tkinter__init__.py",
line 1705, in call
return self.func(*args) TypeError: 'NoneType' object is not callable
This error would repeat until the GUI is closed.
You can use tkinter variable trace function to set up a callback function to be executed whenever the variable is updated. Inside the callback function, you can then update the label based on the value of the variable.
Below is sample code blocks (based on your posted code design) to achieve your goal:
class Gui:
def __init__(self, *args, **kwargs):
...
self.ColorVar = DoubleVar()
...
class StartPage(Frame):
def __init__(self, parent, controller):
...
# register a callback to be executed whenever variable is modified
controller.ColorVar.trace('w', lambda *args: self.IdkChief(controller))
def IdkChief(self, controller):
img = controller.img1 if controller.ColorVar.get() < 2.5 else controller.img2
self.label4.config(image=img)
Related
I have an object which I want to pass to a new frame through a method in another frame class. I found a solution that looked similar to what I was after and tried it, however, the object doesn't get passed. In fact, I got an error message saying "Value after * must be an iterable, not PackingList", where PackingList is the class which the object is made out of. I tried to instead get the "name" attribute of the object and pass it to the next frame, but it just returns an empty tuple. I am really stuck and I appreciate some help.
class App(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 (Homescreen, Menuscreen, Create, ShowUpcoming, ShowAll, OpenList, Search, Edit):
frame = F(container, self, *args, **kwargs)
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
self.show_frame(Homescreen)
def show_frame(self, container, *args, **kwargs):
frame = self.frames[container]
frame.tkraise()
...
class ShowAll(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
tk.Label(self, text = "Alla packningslistor", font = ("Times new roman", 30)).place(x = 110, y = 0)
self.controller = controller
objectList = PK.main()
objectOpen = PK.showAll(objectList)
self.radiobutton_list = []
self.objectList = objectList
for i in range(len(objectOpen)):
radiobutton = tk.Radiobutton(self, text = objectOpen[i], command = functools.partial(self.objectToOpen, idx = i)).place(x = 150, y = 100 + i*30)
self.radiobutton_list.append(radiobutton)
def objectToOpen(self, idx):
objectID = self.objectList[idx]
return self.controller.show_frame(OpenList, *objectID)
class OpenList(tk.Frame):
def __init__(self, parent, controller, *objectID):
tk.Frame.__init__(self, parent)
#lala = getattr(objectID, "name")
#print(lala)
As I said I tried to pass just the name of the object but it prints out as an empty tuple in the next frame class.
I did not understand your question. But here are my corrections to your App class. I hope these corrections can help your understanding of Python and tkinter and debug the rest of your codes.
If you need more detailed help, it will be helpful if you can more specific by stating in the comment section what you want to do with classes App, ShowAll, and OpenList (or their method), and I will see how I can help you by elaborating my answer further.
import tkinter as tk
class App(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__()
self.container = tk.Frame(self)
self.container.pack(side="top", fill="both", expand=True)
self.frames = {}
colors = ('white', 'black', 'red', 'green', 'blue', 'cyan', 'yellow', 'magenta')
frames = ("Homescreen", "Menuscreen", "Create", "ShowUpcoming", "ShowAll", "OpenList", "Search", "Edit")
col=0
for C, F in zip(colors, frames):
print(f'{F=}')
self.frames[F] = tk.Frame(self.container, background=C, width=100, height=50)
self.frames[F].grid(row=0, column=col, sticky="nsew")
# col += 1 # uncomment this to see all frames
self.show_frame('Create') # you should see red
def show_frame(self, container):
self.frames[container].tkraise()
if __name__ == "__main__":
app = App()
app.mainloop()
I'm a teacher and every week I grade students' participation score. Since I teach computers, I thought I'd create a program to handle that logic for me. I plan on using TkInter to create a start screen with the 4 periods of the day, and depending on the period, it would pull up that class. But I'm trying to use the same class for all 4 periods, since the code is exactly the same.
Here's my code:
class part(tk.Tk):
#Creates a class for the GUI
def __init__(self, *args, **kwargs):
#Initialization function of partGUI
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.iconbitmap(self, default="") #default icon in an .ico file
tk.Tk.wm_title(self, "Lucey's Participation Program") #title
window = tk.Frame(self)
window.pack(side="top", fill="both", expand=True)
window.grid_rowconfigure(0, weight=1)
window.grid_columnconfigure(0, weight=1)
self.frames= {}
for F in (StartPage, ClassPart, SettingsPage):
frame = F(window, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, window):
#Allows Program to switch windows/pages/frames
frame = self.frames[window]
frame.tkraise()
class StartPage(tk.Frame):
# Home Screen for Program
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
title = tk.Label(self, text="Participation Scores", font=LARGE_FONT)
title.pack(pady=10, padx=10)
btnPeriod1 = tk.Button(self, text="1st Period", fg="red",
command=lambda: controller.show_frame(ClassPart(controller, 1)))
btnPeriod1.pack()
class ClassPart(tk.Frame):
# Screen showing students, participation buttons & their scores/Hogwarts houses
def __init__(self, parent, controller, period):
tk.Frame.__init__(self, parent)
But this throws an error:
Traceback (most recent call last):
File "/home/klucey/Documents/partvers2.py", line 307, in <module>
window = part()
File "/home/klucey/Documents/partvers2.py", line 40, in __init__
frame = F(window, self)
TypeError: __init__() missing 1 required positional argument: 'period'
Any help to a beginner/intermediate would be greatly appreciated!
This boilerplate code that you (and everybody else on SO, it seems) are using to handle a multi-page Tkinter app is simply not suitable for handing multiple pages of the same class. You'd have to put multiple occurrences of ClassPart in the page list, and somehow arrange for them to be given a different period parameter when constructed - but this breaks the .show_frame() method, as you no longer have a unique identifier to select the page to be shown.
Here's what I would suggest instead, given that you have a fixed set of pages (this wouldn't work so well with dynamically-generated pages):
Get rid of the period parameter in your class (so that its constructor is compatible with the other pages).
Create a subclass of it for each period:
class ClassPart1(ClassPart):
period = 1
class ClassPart2(ClassPart):
period = 2
... and so on. Refer to self.period in the base class to access this value.
Change the initial page creation loop to for F in (StartPage, ClassPart1, ClassPart2, ClassPart3, ClassPart4, SettingsPage):. Use ClassPart1, etc. as the identifier to pass to .show_frame().
I'm trying to call a String from a entry box in another class to a public Var. The code for calling the Var seams to work fine but got a Error ''' image "pyimage5" doesn't exist''' on my second class sins i added the new lines of code. Everything was working fun before.
Lines of code I added:
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent,bg=Gray_Back_Page_1)
to
class StartPage(tk.Frame):
def __init__(self, parent, controller):
super(StartPage,self).__init__()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
super(PageOne,self).__init__(parent,bg=Gray_Back_Page_1)
and added the following code outside a class
StartPage_object = StartPage(tk.Frame, SeaofBTCapp)
USER = StartPage_object.Username_Text.get()
PASSWORD = StartPage_object.Password_Text.get()
The image code Var = Image.open(Image_File +"\File_Name.png") seams to work in my Start Page but gives me an Error in my Page One
Please see complete code below:
desktop = os.path.expanduser("~\Desktop")
Image_File = os.path.expanduser("~\Desktop\file")
#===============Frame==========================Container====================>
class SeaofBTCapp(tk.Tk,object):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Name")
tk.Tk.geometry(self,"1360x728")
tk.Tk.iconbitmap(self, default= desktop + "\icon.ico")
self.resizable(False, False)
#tk.Tk.overrideredirect(False)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=10)
container.grid_columnconfigure(0, weight=10)
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()
#===============Start=========================Page====================>
class StartPage(tk.Frame):
def __init__(self, parent, controller):
super(StartPage,self).__init__()
#=============Set_Background==================>
load1 = Image.open(Image_File +"\Login_Bcakground.png")
render1 =ImageTk.PhotoImage(load1)
#========= Reset_username & Password ===========>
self.Username_Text= tk.StringVar()
self.Password_Text= tk.StringVar()
#==== Creating Buttons, Entry Box and Labels with there commands =====>
User_Name_Entry = tk.Entry(self,textvariable = self.Username_Text, fg =
Entry_Box_Text_Colour,bg =
Entry_Box_Back_White,borderwidth = 0,
font=Normal_Text,width = 30)
User_Name_Entry.place(x=795,y=282)
User_Pass_Entry = tk.Entry(self,textvariable = selfPassword_Text, fg =
Entry_Box_Text_Colour,bg =
Entry_Box_Back_White,borderwidth = 0,
font=Normal_Text,width = 30)
User_Pass_Entry.place(x=795,y=329)
#===============Login==========================Var====================>
StartPage_object = StartPage(tk.Frame, SeaofBTCapp)
USER = StartPage_object.Username_Text.get()
PASSWORD = StartPage_object.Password_Text.get()
#===============Page==========================One====================>
class PageOne(tk.Frame):
def __init__(self, parent, controller):
super(PageOne,self).__init__(parent,bg=Gray_Back_Page_1)
photo = ImageTk.PhotoImage(Image.open(Image_File +"\Splach_Page.png"))
vlabel=tk.Label(self,text = "",image=photo)
vlabel.image = photo
vlabel.place (x=-1,y=-5,relwidth=1, relheight=1)
Error Lines:
image "pyimage5" doesn't exist
Stack trace:
> File "C:\Users\MainUser\source\v_1_1.py", line 251, in __init__
> vlabel=tk.Label(self,text = "",image=photo)
> File "C:\Users\MainUser\source\v_1_1.py", line 93, in __init__
> frame = F(container, self)
> File "C:\Users\MainUser\source\v_1_1.py", line 2736, in <module>
> app = SeaofBTCapp()
Loaded '__main__'
for me, the problem was with Spyder not the code.
I changed to Jupyter Notebook and it worked as expected
This looks like a known bug in tkinter, where the python garbage collector deletes the photo. To prevent this, you need to create a global reference to the image. Something like this should work:
class PageOne(tk.Frame):
images = []
def __init__(self, parent, controller):
super(PageOne,self).__init__(parent,bg=Gray_Back_Page_1)
photo = ImageTk.PhotoImage(Image.open(Image_File +"\Splach_Page.png"))
PageOne.images.append(photo)
vlabel=tk.Label(self,text = "",image=photo)
vlabel.image = photo
vlabel.place (x=-1,y=-5,relwidth=1, relheight=1)
Hope that works for you!
This did not work and give me the same error, but your idea helped me to solve it in another way. I went back to my original code and solved my first issue with the list methods.
#=============Use_List_For_Capturing_Var_In_Classes==================
Name_oF_User_Loged_In = []
Name_oF_User_Loged_In.append("User Name")
class StartPage(tk.Frame):
global login_data_Pass
global login_data_User
global Name_oF_User_Loged_In
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
#=============Set_Background==================>
load1 = Image.open(Image_File +"\Login_Bcakground.png")
render1 =ImageTk.PhotoImage(load1)
def Login_Check():
USER1 = self.Username_Text.get()
PASSWORD1 = self.Password_Text.get()
login_data_Pass.pop(0)
login_data_User.pop(0)
login_data_Pass.append(PASSWORD1)
login_data_User.append(USER1)
#==== Creating Buttons, Entry Box and Labels with there commands =====>
Login_button = tk.Button(self, text="Login",
fg="#d0cece",bg="#3f9a84",borderwidth = 0, font=Normal_Text,height
=1,width = 10,
command=lambda: Login_Check())
Login_button.place(x=1010,y=380)
User_Name_Entry = tk.Entry(self,textvariable = self.Username_Text, fg =
Entry_Box_Text_Colour,bg =
Entry_Box_Back_White,borderwidth = 0,
font=Normal_Text,width = 30)
User_Name_Entry.place(x=795,y=282)
User_Pass_Entry = tk.Entry(self,textvariable = selfPassword_Text, fg =
Entry_Box_Text_Colour,bg =
Entry_Box_Back_White,borderwidth = 0,
font=Normal_Text,width = 30)
User_Pass_Entry.place(x=795,y=329)
class PageOne(tk.Frame):
global login_data_Pass
global login_data_User
global Name_oF_User_Loged_In
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent,bg=Gray_Back_Page_1)
photo = ImageTk.PhotoImage(Image.open(Image_File +"\Splach_Page.png"))
vlabel=tk.Label(self,text = "",image=photo)
vlabel.image = photo
vlabel.place (x=-1,y=-5,relwidth=1, relheight=1)
user = login_data_User[0]
password = login_data_Pass[0]
I would like to refresh the MainPage when the updateCustomerList is finished updating the list so that this updated list is shown on the MainPage widgets.
I tried playing around with tk.show_frame(<frame>) and etc. but since the function itself isn't tied to the main Tkinter frame itself or isn't even a Tkinter object, then I'm not entirely sure how to reload the page. Any suggestions?
The code below is a snippet of my entire program:
customerList = [] #list is updated at the updateCustomerList function; global variable
class POS(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 (ErrorPage, PaymentPage, MainPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column = 0, sticky = "nsew")
#show frame here
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
"""
Tkinter frame that I would like to "refresh" and use the new updated list
"""
frame5Button = ttk.Button(frame5, text = "Add Item", command = lambda: updateCustomerList(barCode, quantity))
frame5Button.grid(row = 0, column = 5, padx = 90, pady = 10)
#This button allows me to go into the updateCustomerList function
def updateCustomerList(barCode, quantity):
#some code to update a list
#when function finishes updating the list, I would like to go back to the MainPage Tk frame and reload all the widgets like labels and entry boxes using the updated customerList list
app = POS()
app.geometry("700x700")
app.resizable(False, False)
app.after(100, MasterFilePopUp)
app.mainloop()
Just remove and recreate the instance of Mainframe inside as the last line of updateCustomerList:
container = 0 #global variable
Add "global container" inside POS(tk.Tk)
def updateCustomerList(barCode, quantity):
global app
...
app.frames[MainPage].destroy()
app.frames[MainPage] = MainPage(container, app)
app.frames[MainPage].grid(row=0, column = 0, sticky = "nsew")
app.frames[MainPage].tkraise()
#function ends here
So I'm trying to handle two windows in python tkinter that can edit an xml file containing the path to certain images, the problem is the function that reads the file is in the __init__ of my two class windows so whenever I switch between the windows the same images appear. My question is: is there any way to relaunch the classes so that the __init__ will run?
I believe the problem is within the show_frame function since tkraise does not run the functions in the class but just pops whatever is in the class to the top.
class xmleditApp(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 (FirstWindow, SecondWindow):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(FirstWindow)
def show_frame(self, cont):
frame=self.frames[cont]
frame.tkraise()
This is the class that I want to update automatically when I return to it:
class FirstWindow(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
secondwindow=tk.Button(self,text="edit xml",command=lambda: controller.show_frame(SecondWindow))
secondwindow.grid(row=3,column=0)
def getimagepath():
doc = parse('pruebasXML.xml')
paths = doc.getroot()
path = paths.findall('path')
pasthlist = []
for elem in range (0,len(path)):
pathlist+=[[]]
for tag in range (0,11):
pathlist[elem]+=[path[elem][tag].text]
return createbutton(pasthlist)
def createbutton(lst):
for elem in range(0,len(lst)):
buttonName=elem
photo=tk.PhotoImage(file=lst[elem][0]+'.gif')
buttonName=tk.Button(self, image=photo)
buttonName.grid(row=2, column=elem)
buttonName.image=photo
if os.path.exists('pathsXML.xml')==True:
getimagepath()
If you have a class called myclass, you can manually call __init__ by using myclass.__init__(self, etc). Here, self is the self argument, and etc are the other arguments.