Python Forum
[Tkinter] Update variable between class/frame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Update variable between class/frame
#1
Hello,

I have a problem with Tkinter to exchange and update variable. I tried inheritance, doing an other interface...)

Below an example
import tkinter as tk
from tkinter import ttk


LARGE_FONT= ("Verdana", 12)


class Mainapp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        #tk.Tk.iconbitmap(self,default='clienticon.ico')
        tk.Tk.wm_title(self, "Sea of BTC Client")

        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):

            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(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        self.VARIABLEtoCHANGE=IntVar()
        self.VARIABLEtoCHANGE=1
        
        label = ttk.Label(self, text="Start Page", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button = ttk.Button(self, text="Go to change value on Page 1",
                            command=lambda: controller.show_frame(PageOne))
        button.pack()

     
        label=ttk.Label(self,text=self.VARIABLEtoCHANGE).pack()
        

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        self.VARIABLEtoCHANGE=2
        label = ttk.Label(self, text="Page One!!!", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = ttk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(StartPage))
        button1.pack()

        button2 = ttk.Button(self, text="Change Value",
                            command=lambda: controller.StartPage.VARIABLEtoCHANGE)
        button2.pack()



app = Mainapp()
app.mainloop()
Thanks for your help
Reply
#2
could you please elaborate.
Reply
#3
I create 2 frames/classes.
When I arrive in the first One I have a label displaying a variable to 1.
Then I go to the 2nd page/classe and clic on a button that should change the variable from the first classe and Especially modify the label on the first page.

But I cannot manage to change this label
Reply
#4
StartPage hasn't been instantiated in Class PageOne
also, line 41 should read: self.VARIABLEtoCHANGE=tk.IntVar()
Reply
#5
It still doesn't work. The error in the console is the following:
Error:
Exception in Tkinter callback Traceback (most recent call last): File "...\anaconda3\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File ".../test.py", line 70, in <lambda> command=lambda: controller.StartPage.VARIABLEtoCHANGE) File "...\anaconda3\lib\tkinter\__init__.py", line 2101, in __getattr__ return getattr(self.tk, attr) AttributeError: '_tkinter.tkapp' object has no attribute 'StartPage'
        
class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        self.VARIABLEtoCHANGE=tk.IntVar()
        self.VARIABLEtoCHANGE=1
        
       ...

        label=ttk.Label(self,text=self.VARIABLEtoCHANGE).pack()
        

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        self.VARIABLEtoCHANGE=tk.IntVar()
        self.VARIABLEtoCHANGE=2
        
        ...
        button2 = ttk.Button(self, text="Change Value",
                            command=lambda: controller.StartPage.VARIABLEtoCHANGE)
        button2.pack()



app = Mainapp()
app.mainloop()
Reply
#6
same issue. StartPage must be instantiated in class PageOne before it can be used.
Reply
#7
It tired something but it doesn't works.
class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        self.VARIABLEtoCHANGE=tk.IntVar()
        self.VARIABLEtoCHANGE=1
        
       ...

        button = ttk.Button(self, text="Go to change value on Page 1",
                            command=lambda: controller.show_frame(PageOne))
        button.pack()

     
        label=ttk.Label(self,text=self.VARIABLEtoCHANGE).pack()
        

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        X=StartPage(self,tk.Frame)
        self.VARIABLEtoCHANGE=tk.IntVar()
        self.VARIABLEtoCHANGE=2
        
        ...

        button1 = ttk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(X))
        button1.pack()

        button2 = ttk.Button(self, text="Change Value",
                            command=lambda: X.VARIABLEtoCHANGE)
        button2.pack()
The point is that i don't understand the logical.

The step are the following:
  1. I have a main window that initiate all tkFrame and create StartPage and PageOne.
  2. Then i launch the startpage which has a variable set to 1.
  3. This StartPage can display the PageOne by clicking on a button
  4. On PageOne if i create an instance X of StartPage, it means that this is a new object different from the one create before for StartPage. So the variable of StartPage and X would be not the same variable.

Now the problem comes from the return to the startPage because X is not declared in the mainPage...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Take the variable of a cursor class delcencen 2 1,189 Feb-13-2023, 05:19 AM
Last Post: deanhystad
  [Tkinter] Update variable using tkinter entry methon drSlump 6 5,200 Oct-15-2021, 08:01 AM
Last Post: drSlump
  update text variable on label with keypress knoxvilles_joker 3 4,902 Apr-17-2021, 11:21 PM
Last Post: knoxvilles_joker
  How can I pass a variable to another class, especially in this situation? Jionni 4 8,049 Feb-21-2021, 02:40 PM
Last Post: deanhystad
  Getting a variable from one class to another class menator01 6 3,148 Apr-26-2020, 04:36 PM
Last Post: Larz60+
  Updating a variable within a class MC2020 2 2,580 Apr-17-2020, 11:31 AM
Last Post: MC2020
  [Tkinter] Scrollbar, Frame and size of Frame Maksim 2 9,003 Sep-30-2019, 07:30 AM
Last Post: Maksim
  [Tkinter] create and insert a new frame on top of another frame atlass218 4 11,116 Apr-18-2019, 05:36 PM
Last Post: atlass218
  [Tkinter] can't update label to include variable foxtreat 2 3,653 Dec-01-2018, 07:16 AM
Last Post: jfong
  [Tkinter] Frame size only works if frame is empty(Solved) Tuck12173 7 6,447 Jan-29-2018, 10:44 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020