Jeremiah
Jeremiah

Reputation: 3

tkinter entry widget textvariable will not update with .set method

My problem is, I cant get the Entry field to display anything.

I have been working with Tk for Python and Ruby for a few years on and off at work making tools for my group. the current tool that I am developing requires some popup windows with Entry widget fields in them. I have employed Entry fields with textvariable attributes in many previous applications, however in this case I am using a Class definition to create generic window instances. I am using StringVar to provide the object for holding and managing the value in the Entry field however no-matter what I set the value of the StringVar to the field will not update. Also setting the Entry to display with text="something" on initialization doesn't display anything in the Entry field either.

here is a simplified example script exhibiting my problem.

import Tkinter
from Tkinter import *

window_list = []

class popup_window:
    def __init__(self):
        self.popup = Tk()
        Entry(self.popup, textvariable=entry_value, width=60, bg="blue", foreground="cyan").grid(row=1, column=1)

def spawner():
    window_list.append(popup_window())

main_window = Tk()
entry_value = StringVar()
entry_value.set("test")
spawn_window = Button(main_window, text='spawn', command=spawner).grid(row=1, column=1)
main_window.mainloop()

When run this script's Entry field does not display "test".

Upvotes: 0

Views: 822

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385820

You cannot create more than once instance of Tk, because of exactly this type of behavior. If you need a new window, create an instance of Toplevel.

Upvotes: 1

Related Questions