简体   繁体   English

带有tkinter的python3:从函数调用小部件

[英]python3 with tkinter: call widget from a function

I'm having a problem with this code: 我对此代码有疑问:

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

(I left the code for the design(bg, border, ...) out) (我省略了设计代码(bg,border等)。)

This is another window which will be started by the main one. 这是另一个窗口,将由主窗口启动。 It shows some buttons on the left and some labels and textboxes on the right. 它在左侧显示一些按钮,在右侧显示一些标签和文本框。

Whenever a button on the left has been pushed the text of the labels should be changed. 只要按下左侧的按钮,就应该更改标签的文本。

That's the problem: When I push a button I get this error and the text won't be changed: 这就是问题所在:按下按钮时出现此错误,并且文本不会更改:

Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.4/tkinter/__init__.py", line 1536, in __call__
return self.func(*args)
File "/[...]/program.py", line 27, in colormain
lbl_0.config(text="Background")
NameError: name 'lbl_0' is not defined

I don't really understand why this gives me an error so I'd like to ask you. 我真的不明白为什么这会给我一个错误,所以我想问你。

This code is being started from the main window with the code: 该代码是从主窗口使用以下代码启动的:

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

Thanks in advance. 提前致谢。

Do not declare and pack in the same line 不要在同一行中声明和打包

Return of this peice of code is None 此代码的返回为None

Label(win_0, text="Option0").pack()

whereas, this returns an object of Label class 而这将返回Label类的对象

Label(win_0, text="Option0")

so use:- 因此使用:-

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

instead of 代替

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

Also use self object as argument to functions. 还可以使用self对象作为函数的参数。 Check that the variables are in scope wherever you are using it. 无论您在何处使用变量,均应检查其范围。 This should help you get through this error... 这应该可以帮助您解决此错误...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM