Home > Software design >  How can i import a function into class of an external file? NameError: name 'clicked' is n
How can i import a function into class of an external file? NameError: name 'clicked' is n

Time:01-01

If i click on the checkbox, i get this error:

 command=lambda: clicked(Checkbutton1.get(), Button1_func))
NameError: name 'clicked' is not defined

because the checkbox is in the external file and requires command=lambda: clicked(Checkbutton1.get(), Button1_func)). I need the code setup to stay like mine, i.e. with the function clicked in the main file and not in page1, and and the use of variuous lambda.

I've tried importing clicked (as a parameter) in several ways, such as adding it into def __init__, but it doesn't work.

How can I fix? Thank you

Main file

import tkinter as tk
from tkinter import ttk
from page1 import *

root = tk.Tk()
root.geometry('480x320')

style = ttk.Style()
style.theme_use('default')
style.configure('TNotebook', tabposition='ws', background='white', tabmargins=0)

nb = ttk.Notebook(root)
nb.pack(fill='both', expand=1)
page1 = Page1(nb)
nb.add(page1, text='Page 1', compound='left')

datalist = []
        
def clicked(flag, func):
    if flag:
        datalist.append(func())
    else:
        datalist.remove(func())

def try_print():
    if len(datalist) > 0:
        print("ok")

button = tk.Button(root, text="Print", command= try_print())
button.place(x=60, y=100)

root.mainloop()

Page1 file

import tkinter as tk
from tkinter import ttk

class Page1(tk.Frame):
    def __init__(self, master, **kw):
        super().__init__(master, **kw)

        Checkbutton1 = tk.IntVar()

        def Button1_func():
            x = "test"
            return x
  
        Button1 = tk.Checkbutton(self, text = "Checkbox1", variable = Checkbutton1, onvalue = 1, offvalue = 0, height = 1,
                                 bg="white", foreground='black', activebackground="white", highlightthickness = 0,
                                 command=lambda: clicked(Checkbutton1.get(), Button1_func))
        Button1.place(x=10, y=30)

CodePudding user response:

It is more easier to pass clicked function to Page1 as an argument:

Main file:

import tkinter as tk
from tkinter import ttk
from page1 import Page1

def clicked(flag, func):
    if flag:
        datalist.append(func())
    else:
        datalist.remove(func())
    print(datalist)

def try_print():
    if len(datalist) > 0:
        print("ok")


root = tk.Tk()
root.geometry('480x320')

style = ttk.Style()
style.theme_use('default')
style.configure('TNotebook', tabposition='ws', background='white', tabmargins=0)

nb = ttk.Notebook(root)
nb.pack(fill='both', expand=1)

page1 = Page1(nb, clicked) # pass clicked as an argument
nb.add(page1, text='Page 1', compound='left')

datalist = []

button = tk.Button(root, text="Print", command=try_print)
button.place(x=60, y=100)

root.mainloop()
page1 file
import tkinter as tk
from tkinter import ttk

class Page1(tk.Frame):
    # added clicked argument
    def __init__(self, master, clicked, **kw):
        super().__init__(master, **kw)

        Checkbutton1 = tk.IntVar()

        def Button1_func():
            x = "test"
            return x
  
        Button1 = tk.Checkbutton(self, text = "Checkbox1", variable = Checkbutton1, onvalue = 1, offvalue = 0, height = 1,
                                 bg="white", foreground='black', activebackground="white", highlightthickness = 0,
                                 command=lambda: clicked(Checkbutton1.get(), Button1_func))
        Button1.place(x=10, y=30)
  • Related