2

Widget tk.Entry from example_script.py do not save value 'textvariable' field.

example_script.py:

import Tkinter as tk

class App(tk.Frame):
    def __init__(self, master, text):
        tk.Frame.__init__(self, master)
        textVar = tk.StringVar()
        textVar.set(text)
        entryVar = tk.Entry(self, textvariable=textVar).pack()
        self.pack()

def main():
    root = tk.Tk()
    text = ['text1', 'text2', 'text3']
    for i in text:
        App(root, i)
    root.mainloop()

main_script.py:

import Tkinter import example_script as ex

if __name__ == '__main__':
    root = Tkinter.Tk()
    Tkinter.Button(root, text='press', command=lambda: ex.main()).pack()
    root.mainloop()

If I change row 'entryVar = tk.Entry(self, textvariable=textVar).pack()' to

entryVar = tk.Entry(self)
entryVar.pack()
entryVar.insert(0, text)

field's value is updated. Why?

How will be correct open new window from imported script? Tkinter.Toplevel() is not suitable. Now I use subprocess.Popen.

2 Answers 2

1

When you do entryVar = tk.Entry(self).pack(), entryVar will be set to None because that is what pack() returns. When you call pack on a separate line, entryVar gets set to what you think it does.

Sign up to request clarification or add additional context in comments.

1 Comment

Not this question. In case tk.Entry(self, textvariable=textVar).pack() or entryVar = tk.Entry(self, textvariable=textVar); entryVar.pack() text 'text1', 'text2' etc is not displayed. In case usage entryVar.insert(0, text) text is displayed.
0

You cannot create two instances of the Tk class in one program. Tkinter is not designed to work that way.

3 Comments

I know. Therefore, this different behavior is interesting. My story: there is a script that can not be changed. The script creates a window. Now I call it from other window (when button pressed) using module subprocess (Popen). Are there other ways?
What you are asking in your comment is a completely different question. If you're using popen to spawn another process, that's a completely different issue from the original question.
Sorry. Probably should have been split into two questions. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.