0

I have the following code (example of my real program):

from tkinter import *
def class1(Frame)

    def nv(self,x):
        self.vent=Toplevel(self.master)
        self.app=class2(self.vent)
        self.value=x

    def __init__(self,master):
        super().__init__(master)
        self.master=master
        self.frame=Frame(self.master)
        self.btn=Button(self, text="example", command=lambda: self.nw(1))
        self.btn.pack()
        self.pack()

def class2(Frame):
    def __init__(self, master):
        super().__init__(master)
        self.master=master
        self.frame=Frame(self.master)
        self.value=class1.nw.value.get()
root= Tk()
marco=Frame(root)
marco.pack
lf=class1(marco)
root.mainloop()

this last part is the problem, i cant use .get() properly for this problem, I want to get the value of x when I create the new window. I use lambda so i can execute the command with parameters. So the question is, is there a way for me to access the value of x in class 2?

1 Answer 1

1

You appear to be quite confused about the usage of classes when using tkinter. super() should not be used with tkinter as explained here and when declaring a class you should use the class keyword, not def. .get() is a method of a tkinter variable class such as tkinter.IntVar, tkinter.StringVar, etc. so is not needed in the example you have given.

What you need is the function in the Frame you are trying to get the value of x from (nv) and then parse that value to the __init__ method in the child Frame.

Here is my solution:

from tkinter import *

class Class1(Frame):

    def nv(self,x):
        self.vent = Toplevel(self.master)
        self.app = Class2(self.vent,x)

    def __init__(self,master):
        Frame.__init__(self,master)
        self.master = master
        self.btn = Button(self, text="example", command=lambda: self.nv(1))
        self.btn.pack()
        self.pack()

class Class2(Frame):

    def __init__(self, master, x):
        Frame.__init__(self,master)
        self.master=master
        self.frame=Frame(self.master)
        self.x_text = Label(self, text=str(x))
        self.x_text.pack()
        self.pack()

root = Tk()
marco = Frame(root)
marco.pack()
lf = Class1(marco)
root.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

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.