简体   繁体   English

python tkinter和stringvar()的范围

[英]python tkinter and scope of stringvar()

Python noob here. Python noob在这里。 I've created a simple tkinter app with 6 frames. 我创建了一个简单的6帧tkinter应用程序。 Using multiple frames was the easiest way I could find to get all of the widgets to line up. 使用多个框架是让所有小部件排列起来的最简单方法。

I want to have a text entry field in one of the frames that can be updated by other functions in the code. 我想在其中一个框架中有一个文本输入字段,可以通过代码中的其他功能进行更新。 However, no matter what I try, I cannot get blah.set( "blah" ) to work. 但是,无论我尝试什么,都无法使blah.set(“ blah”)正常工作。

I'm not using the self/parent stuff because I have not been able to figure that out yet. 我没有使用自我/父母的东西,因为我还不能弄清楚。 Here's what I've got: 这是我得到的:

def Next () :

    glob_current_company_display.set( "test" )


def makeWindow () :

    win = Tk()
    win.title('Finder')
    win.geometry('+842+721')

    # ************************************************************************
    # Frame 2
    frame2 = Frame(win)
    button_03 = Button(frame2, text="Next", width=10, command=Next)
    button_03.pack(side=LEFT)

    # ************************************************************************
    # Frame 5
    frame5 = Frame(win)
    co_name_label = Label(frame5, text="Company Name: ", justify=LEFT)
    co_name_label.pack(side=LEFT)

    global glob_current_company_display 
    glob_current_company_display = StringVar()
    co_name_entry = Entry(frame5, width=50, textvariable=glob_current_company_display)

    co_name_entry.pack(side=LEFT)

    # ************************************************************************
    # Pack the frames

    frame5.pack(side=TOP, pady=5, padx=5)
    frame2.pack(side=TOP, pady=5, padx=5)

    return win


win = makeWindow()

win.mainloop()

You probably should investigate the "self/parent stuff". 您可能应该调查“自我/父母的东西”。 It's comes in very handy for this kind of stuff. 这种东西非常方便。 For now: you could put the creation of the global variable before the button_03 that has Next() as command (which has a reference to te global in it). 现在:您可以将全局变量的创建放在具有Next()作为命令的button_03之前(在其中引用te global)。

from Tkinter import *

def Next () :
    glob_current_company_display.set( "test" )

def makeWindow () :



    win = Tk()
    win.title('Finder')
    win.geometry('+842+721')

    global glob_current_company_display 
    glob_current_company_display = StringVar()

    # ************************************************************************
    # Frame 2
    frame2 = Frame(win)
    button_03 = Button(frame2, text="Next", width=10, command=Next)
    button_03.pack(side=LEFT)

    # ************************************************************************
    # Frame 5
    frame5 = Frame(win)
    co_name_label = Label(frame5, text="Company Name: ", justify=LEFT)
    co_name_label.pack(side=LEFT)


    co_name_entry = Entry(frame5, width=50, textvariable=glob_current_company_display)

    co_name_entry.pack(side=LEFT)

    # ************************************************************************
    # Pack the frames

    frame5.pack(side=TOP, pady=5, padx=5)
    frame2.pack(side=TOP, pady=5, padx=5)

    return win

win = makeWindow()

win.mainloop()

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

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