0

I'm having a simple tkinter two frame application with a Label, Entry and Button widget and I want to access a StringVar() of FrameOne with a Entry and Button of FrameTwo.

If have seen a lots of examples of code, but do not get how this is been done in my example below. Many programmers are using a controller. If I would use a controller, I end up from an error to another. For example:

FirstFrame = FrameOne(mainWindow)`

TypeError: __init__() missing 1 required positional argument: 'controller'

Which I completely understand, because I do not pass anything into the new 'controller' class argument when calling the Frame class. But I do not know what I should pass into this to solve it. Perhaps it is also caused by the lack of knowledge of using class variables (any literature tips are welcome).

The same counts for the solution to inherit FrameOne into FrameTwo. I bump into the same amount of errors applying to my code.

Another thing is that many programmers have examples of two frames that are not visible at the same time, while in my example I have two frames underneath each other at the same time.

An different related issue that I have is, what if the label widget of FrameOne was a Text widget? How do I access the widget from FrameTwo.

I could make it work with globals, but I do not want to use such writing and I will keep the access widget problem anyhow.

Please find my code below:

import tkinter as tk

class AppWindow():
    def __init__(self, master):
        self.master = master
        master.title("Test Application")
        master.geometry("1060x680")
        master.grid_propagate(False)


class FrameOne(tk.Frame):
    def __init__(self, parent):
        super().__init__()
        self["borderwidth"]=5
        self["relief"]="ridge"

        self.LabelText = tk.StringVar()
        self.LabelText.set("It is not working yet")

        self.testlabel = tk.Label(self, textvariable=self.LabelText)        
        self.testlabel.grid(row=1, column=1)


class FrameTwo(tk.Frame):
    def __init__(self, parent):
        super().__init__()
        self["borderwidth"]=5
        self["relief"]="ridge"

        self.testentry = tk.Entry(self)
        self.testentry.insert("end", "This should be working")
        self.testentry.grid(row=1,column=1)

        self.testbutton = tk.Button(self, text="Test the label", command=self.updatelabel)
        self.testbutton.grid(row=1,column=2)

    def updatelabel(self):
        FrameOne.LabelText.set(self.testentry.get())   #HOW TO FIX THIS CODE THE RIGHT WAY?


#Create a window as defined in the AppWindow class
mainWindow = AppWindow(tk.Tk()) 

#Create a Frame as defined in class FrameOne
FirstFrame = FrameOne(mainWindow)
FirstFrame.grid(row=0, column=0) #Positioning Frame on Window

#Create a Frame as defined in class FrameOne
SecondFrame = FrameTwo(mainWindow)
SecondFrame.grid(row=1, column=0) #Positioning Frame on Window
4
  • What does happen when you click the button? Commented Apr 15, 2020 at 19:11
  • @martineau: I wish you wouldn't recommend that code to beginners. It's terrible as a learning tool, and has led to countless questions on this site. Commented Apr 15, 2020 at 19:43
  • Does this answer your question? how-would-i-access-variables-from-one-class-to-another Commented Apr 15, 2020 at 20:28
  • Unfortunately, it doesn't. I have read it and it inheritates one class into another. But in my code I already inheritating the tk.Frame. I get an error when I inheritate another class. In my test example, Brian provided me the simples, but correct answer (below). Commented Apr 16, 2020 at 17:53

1 Answer 1

1

Like with any python object, you access an attribute of an object using a reference to the object.

In your case, updatelabel should look like this:

def updatelabel(self):
    FirstFrame.LabelText.set(self.testentry.get()) 

Note: your use of uppercase characters for instance variables makes your code much harder to comprehend. I recommend following the naming guidelines in PEP8.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. How could I have missed this... Thank you for the PEP8 too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.