简体   繁体   中英

python tkinter and scope of stringvar()

Python noob here. I've created a simple tkinter app with 6 frames. 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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