0

I was reading this tutorial of StackOverflow on how to get variables among classes, but I'm not able to make it work. Basically what i want to do is to pass an entry value from StartPage to PageOne where i need it. I also tried "the wrong way" using global variables, but I need an int and I can't covert the string entry to an int. Down below there's my code, can you help me please?

import tkinter as tk                # python 3
from tkinter import font as tkfont
from typing_extensions import IntVar  # 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()

def get_page(self, classname):
    '''Returns an instance of a page given it's class name as a string'''
    for page in self.frames.values():
        if str(page.__class__.__name__) == classname:
            return page
    return None


class StartPage(tk.Frame):

def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    self.controller = controller
    label = tk.Label(self, text="Start Page: insert value", font=controller.title_font)
    label.pack(side="top", fill="x", pady=10)
    
    self.valore = tk.IntVar()
    entry = tk.Entry(self, textvariable=self.valore).pack()
    
    button1 = tk.Button(self, text="Go to Page One",
                        command=lambda: controller.show_frame("PageOne"))
    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)
    self.controller = controller

    start_page = self.controller.get_page("StartPage")
    value = start_page.entry.get()
    print ('The value stored in StartPage entry = %s' % value)
    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.mainloop()
mandiatutti
  • 57
  • 1
  • 10
  • 2
    You can call `start_page.valore.get()` instead of `start_page.entry.get()` *whenever you want to get the entry value* (not in `__init__()`) because `entry` inside `StartPage` is a local variable which cannot be accessed elsewhere. – acw1668 Jul 06 '21 at 09:19
  • @acw1668 Thanks, that kinda works, but actually the value isn't really passed. It opens the new frame by default with 0, and it always passes that value... If I change it and press the button it doesn't update the variable "valore" – mandiatutti Jul 06 '21 at 11:39
  • 1
    As I said that you better not get the value inside `PageOne.__init__()`, may be inside a function tiggered by a button. Or use the variable via `textvariable` option of a `Label` inside `PageOne`. – acw1668 Jul 06 '21 at 11:52
  • But that I can't call 2 functions with a single button. And i'd have to create n buttons where n is the number of entries... I'm struggling to figure out what is the best way to pass a value from page "StartPage" to "PageOne" or Page x... I mean... I found that tutorial that logically create a method to retrieve a value from another page... but why that value isnt updated? – mandiatutti Jul 06 '21 at 12:00
  • What I'm trying to achieve is that when i press page one button, the value i put in the entry is accessible and updated. without creating others button to store the value – mandiatutti Jul 06 '21 at 12:03
  • I would recommend you to learn more about *event driven programming*, then you will know in what situation you can get the value correctly. – acw1668 Jul 06 '21 at 12:21
  • Do you have any reference? – mandiatutti Jul 06 '21 at 12:44

0 Answers0