python3 with tkinter:从函数调用小部件

python3 with tkinter:从函数调用小部件,python,python-3.x,user-interface,tkinter,Python,Python 3.x,User Interface,Tkinter,我对此代码有问题: from tkinter import * class app: def create(arrSettings): proot = Toplevel() proot.title("Settings") m = Frame(proot).pack() #Some Frames so I can arrange them how I'd like to mcan =

我对此代码有问题:

from tkinter import *

class app:
    def create(arrSettings):
            proot = Toplevel()
            proot.title("Settings")

            m = Frame(proot).pack()    #Some Frames so I can arrange them how I'd like to
            mcan = Canvas(proot)
            mcan.pack(fill="both", side="left")

            x = Frame(proot).pack()
            xcan = Canvas(proot)
            xcan.pack(fill="both", expand="yes", side="left")

            win_0 = Frame(xcan)
            lbl_0 = Label(win_0, text="Option0").pack()
            txt_0 = Text(win_0).pack()
            win_0.pack()

            win_1 = Frame(xcan)
            lbl_1 = Label(win_1, text="Option1").pack()
            txt_1 = Text(win_1).pack()
            win_1.pack()


            btn_menu0 = Button(mcan, text="Menu0", command=app.func_btn_menu0).pack()
            btn_menu1 = Button(mcan, text="Menu1", command=app.func_btn_menu1).pack()


    def func_btn_menu0():
            lbl_0.config(text="foo")     # <-- Problem
            txt_0.insert("end", "bar")   # <-- Problem

    def func_btn_menu1():
            pass
我真的不明白为什么这会给我一个错误,所以我想问你

此代码从主窗口启动,代码如下:

program.app.create(arrSettings) #arrSettings is an array in which some colors for the design are

提前谢谢。

请勿在同一行申报和包装

此代码集的返回为“无”

Label(win_0, text="Option0").pack()
然而,这将返回一个Label类的对象

Label(win_0, text="Option0")
因此,请使用:-

lbl_0 = Label(win_0, text="Option0")
lbl_0.pack()
而不是

lbl_0 = Label(win_0, text="Option0").pack()
也可以使用self对象作为函数的参数。检查变量是否在使用它的任何地方的范围内。
这将有助于您克服此错误…

看起来您正试图访问超出范围的lbl_0和txt_0变量。发送self对象,然后尝试这样做感谢您的帮助
lbl_0 = Label(win_0, text="Option0").pack()