0

My goal is to make a pop-up window that gets the strings from Entry widgets, the number of widgets is determined by the user on a primary window. So if the user inputs 5 fields are required then the pop-up should have 5 Entry widgets to get 5 different strings, if the user inputs 4 the pop-up has 4 widgets and so forth. Using help from this question I wrote the following code:

import tkinter as tk

root = tk.Tk()
root.title("Main Window")

class EntryButton(object):
    def __init__(self, rootWin, numberEn):
        self.number = numberEn
        self.entry = tk.Entry(rootWin)
        self.button = tk.Button(rootWin, text="test", command=self.on_click)
        self.entry.grid(row=numberEn, column=0)
        self.button.grid(row=numberEn, column=1)
        self.value = None

    def on_click(self):
        self.value = self.entry.get()

def popup_window(numberOfEntries, storageDict):
    window = tk.Toplevel()
    window.title("Pop-Up Window")

    dictionary = dict()
    for i in range(0, numberOfEntries):
        dictionary[i] = EntryButton(window, i)
    
    storageDict = dictionary

numberVar = tk.IntVar()
tk.Label(root, text='Enter how many markers you are using:', font=('bold', 10)).grid(column= 0, row= 3)
tk.Entry(root, textvariable=numberVar).grid(column= 1, row=3)
number = numberVar.get()

mainStorage = dict()
initialPlotBtn = tk.Button(root, text='Start Labelling')
initialPlotBtn.config(command =lambda:
    popup_window(numberOfEntries=number, storageDict=mainStorage)
)
initialPlotBtn.grid(column = 1, row = 4)

root.mainloop()

for key in mainStorage:
    print(key, mainStorage[key])

My main window works as seen here: MainWindowImg. However once creating the pop-up window it shows empty like this: Pop-upWindowImg.

I'm merely beginning to use tkinter, so there might be something i'm not getting here. Using Python 3.8.3 on Windows 10

0

1 Answer 1

1

You used value of number as an argument to popup_window() but number is zero because it is assigned right after the entry is created.

You should use numberVar.get() as the argument instead:

initialPlotBtn.config(command =lambda:
    popup_window(numberOfEntries=numberVar.get(), storageDict=mainStorage)
)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it's such a small oversight now that I see it

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.