Search code examples
pythonpython-3.xtkintertkinter-entry

tkinter Entry to StringVar() binding do not work


i have a issue, that i did not figured out why.. All needed code is summarized in this screenshot: Screenshot_Code

Please do not look for sense, i trimmed the code as far as possible, to show the problem only.

My Issue as is follows:

  • If i run main.py
  • Then click Button according to "ui_root.py"
  • and then input some text in the Entry-Field and click Button according to "create_user.py"
  • Then my print Statement is empty, respectivly the StringVar() binding do not work

Bu if i run "create_user.py" directly (uncomment UiCreateUser()) then my print-Statement prints exactly the content/text from my Entry-Widget.

Why? I dont get it, i would apprciate it if someone can help me out.. thanks in advance.

The code for copy and paste purpose(for read use screenshot, it may be better than this):

main.py

from ui_root import UiRoot
app = UiRoot()
app.mainloop()

ui_root.py

from tkinter import Tk, Button
from pixela_brain import PixelaBrain

class UiRoot(Tk):

    def __init__(self):
        super().__init__()
        self.brain = PixelaBrain()

        self.button = Button(self, text="Button", command=self.click)
        self.button.pack()

    def click(self):
        self.brain.create_user()

pixela_brain.py

from popup_windows import create_user
import json


class PixelaBrain:
    def __init__(self):
        pass

    def create_user(self):
        self.new_user = create_user.UiCreateUser()

create_user.py

from tkinter import Tk, Entry, Button, StringVar


class UiCreateUser(Tk):
    def __init__(self):
        super().__init__()

        # Entry
        self.entry_var = StringVar()
        self.entry = Entry(self, textvariable=self.entry_var)
        self.entry.pack()

        # Button
        self.button = Button(self, text="Click", command=self.btn_click)
        self.button.pack()

        self.mainloop()

    def btn_click(self):
        print(self.entry_var.get())


# UiCreateUser()

Solution

  • This as an "answer" because the input from jasonharper (in the comments above) solved my problem and i want to close the question.

    Just replacing class UiCreateUser(Tk) with class UiCreateUser(Toplevel) solved the problem.

    Many thanks :)