A
A
AleksKc2019-04-16 15:04:56
Python
AleksKc, 2019-04-16 15:04:56

How to pass from a class to a function outside the class in OOP?

The program works with a GUI interface. It is necessary to pass a variable from the class to the function above. But I got completely confused with the syntax, and the script gives an error that it does not see this variable.
When you use Radiobutton1 or Radiobutton2 (lines 41 and 45), the script displays an error - NameError: name 'lbl' is not defined
How can I fix it?
The code itself:

import sys
from tkinter import *
import tkinter as tk
import tkinter.ttk as ttk


def vp_start_gui():
    global val, w, root
    root = tk.Tk()
    top = Toplevel1(root)
    root.mainloop()

w = None

def create_Toplevel1(root, *args, **kwargs):
    global w, w_win, rt
    rt = root
    w = tk.Toplevel(root)
    top = Toplevel1(w)
    return (w, top)


def destroy_Toplevel1():
    global w
    w.destroy()
    w = None


def clicked():
    lbl.configure(text=selected.get())
    print(lbl.configure(text=selected.get()))


class Toplevel1:
    def __init__(self, top=None):
        top.geometry("515x311+650+150")
        top.title("New Toplevel")
        top.configure(background="#d9d9d9")

        self.selected = IntVar()
        self.Radiobutton1 = tk.Radiobutton(top, value=0, variable=self.selected, command=clicked)
        self.Radiobutton1.place(relx=0.252, rely=0.257, relheight=0.08, relwidth=0.113)
        self.Radiobutton1.configure(text='''Radio 1''')

        self.Radiobutton2 = tk.Radiobutton(top, value=1, variable=self.selected, command=clicked)
        self.Radiobutton2.place(relx=0.583, rely=0.257, relheight=0.08 , relwidth=0.113)
        self.Radiobutton2.configure(text='''Radio 2''')

        self.Button1 = tk.Button(top)
        self.Button1.place(relx=0.33, rely=0.386, height=64, width=137)
        self.Button1.configure(text='''Button''')
        self.Button1.configure(width=137)

        self.lbl = Label(top)
        self.lbl.grid(column=0, row=1)


if __name__ == '__main__':
    vp_start_gui()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-04-16
@AleksKc

First, you can do without this function at all, just by binding the Label and Button to the same IntVar:

self.selected = IntVar()
self.Radiobutton1 = tk.Radiobutton(top, value=0, variable=self.selected)
self.Radiobutton2 = tk.Radiobutton(top, value=1, variable=self.selected)
self.lbl = Label(top. textvariable=self.selected)

Second, you can make a function a method:
class Toplevel1:
    def __init__(self, top=None):
        ...
        self.Radiobutton1 = tk.Radiobutton(top, value=0, variable=self.selected, command=self.clicked)
        ...

    def clicked(self):
        self.lbl.configure(text=self.selected.get())

Thirdly, you can get by with a lambda:
But if all this is impossible for some reason, then you can pass a reference to the window to the partially applied function:
from functools import partial

...

def clicked(window):
    window.lbl.configure(text=window.selected.get())


class Toplevel1:
    def __init__(self, top=None):
        ...
        self.Radiobutton1 = tk.Radiobutton(top, value=0, variable=self.selected, command=partial(clicked, self))
        ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question