0

I'm setting up a makeshift tkinter window client that looks like this: His further information can be accessed by a secondary window like this:

When I try updating his address by just clicking update, it ends up looking like this:

So I know for a fact that the back end database is not the problem, since I can update the address and what not without any issues just calling the update function from the backend.py script. However, from the front end, when I use a print statement, I found that the entry_value.get() is an empty string whenever I enter the new window.

So I tried globalizing the variable, I tried maybe putting the variable out of the functions and in the main loop instead. I was thinking of using OOP approach, but I believe I shouldn't have to completely break apart my code and change it to OOP just so that entry_value.get() isn't empty.

I've also looked for answers about this matter, but I can't seem to find this particular problem anywhere.

Here's the problem area in my code:

def open_menu():                                 

    global new_window

    new_window = Tk()

    address_entry_value = StringVar()
    address_entry = Entry(new_window, width = 50, textvariable = address_entry_value)
    address_entry.grid(row = 1, column = 1, columnspan = 3)
    address_entry.delete(0, END)
    address_entry.insert(END, "Insert Address Here")
    print(address_entry_value.get())

def get_selected_row(event):
    try:
        global selected_tuple
        global extended_tuple
        index = listbox.curselection()[0]
        selected_tuple = listbox.get(index)
        selected_tuple = selected_tuple.split()
        extended_tuple = backend.select_client(selected_tuple[0])
    except IndexError:
        pass

def open_clients_entry(event):
    open_menu()

window = Tk()

listbox = Listbox(window, width = 75, height = 10)
listbox.grid(row = 1, column = 0, columnspan = 4, rowspan = 7)

listbox.bind("<Double-Button-1>", open_clients_entry)
listbox.bind("<<ListboxSelect>>", get_selected_row)

open_entry_button = Button(window, width = 12, text = "Open Entry", command = open_menu)
open_entry_button.grid(row = 4, column = 5)

window.mainloop()

I'm using "Insert Address Here" for the purpose of using an example instead of using tuples and what not from the database for simplicity in getting to the point of the problem here.

The expected print result in the powershell window should be "Insert Address Here", instead it prints a blank. Hence when I update the address also becomes blank as it takes in an empty address_entry_value.get() like in the pictures below.

This leads to

Any help would be greatly appreciated as I'm really stumped on this problem.

Virb
  • 1,639
  • 1
  • 16
  • 25
JoeShmoe
  • 3
  • 1
  • Don't use more than one instance of `Tk()`. For popup windows use `Toplevel()`. For a detailed answer on *why* not to use multiple instances of `Tk()`, see https://stackoverflow.com/a/48045508/3714930 – fhdrsdg Jun 19 '18 at 09:29
  • Difficult without an mvce but your `print(address_entry_value.get())` code happens as soon as the new window is opened so the user has no time to enter anything before the print happens. – scotty3785 Jun 19 '18 at 09:31
  • @fhdrsdg I'm not quite sure how to flag comments as answers, still really new to stackoverflow, but just changing it to Toplevel() from Tk() was the solution. I will definitely read up more about that subject. Thanks again for the answer – JoeShmoe Jun 19 '18 at 10:44
  • @JoeShmoe You can't. Glad it helped, I'll write it up as an answer in a few minutes. – fhdrsdg Jun 19 '18 at 10:46

1 Answers1

0

You're creating two instances of Tk(). This means that you have two Tcl interpreters, which are completely separated (for more info on this read this answer by Bryan Oakley). You didn't post enough code to exactly see what's happening, but I'm pretty sure it has to do with trying to access information in one Tcl interpreter from the other.

So in general, make sure you only have one instance of Tk(). To have multiple windows which share the same Tcl interpreter, make the second a Toplevel() window.

fhdrsdg
  • 10,297
  • 2
  • 41
  • 62