python 如何将函数导入到外部文件的类中?NameError:未定义名称“clicked”

uklbhaso  于 2023-01-01  发布在  Python
关注(0)|答案(1)|浏览(153)

如果我点击复选框,我会得到这个错误:

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

因为复选框在外部文件中,需要command=lambda: clicked(Checkbutton1.get(), Button1_func))。我需要代码设置保持像我的一样,即在主文件中使用函数clicked,而不是在page1中,并使用各种lambda。
我尝试过用几种方法导入clicked(作为参数),比如将其添加到def __init__中,但都不起作用。
我该怎么修?谢谢

主文件

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()

第1页文件

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)
pgvzfuti

pgvzfuti1#

clicked函数作为参数传递给Page1更容易:

主文件:
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()
第1页文件
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)

相关问题