Accessing dynamically created tkinter widgets

2024/10/11 19:17:36

I am trying to make a GUI where the quantity of tkinter entries is decided by the user.

My Code:

from tkinter import*root = Tk()def createEntries(quantity):for num in range(quantity):usrInput = Entry(root, text = num)usrInput.pack()createEntries(10)root.mainloop()

This code is based on this tutorial i found:

for num in range(10):btn = tkinter.button(window, text=num)btn.pack(side=tkinter.LEFT)

The problem is that I can only access the input in the latest created widget, because they all have the same name. Is there a way of dynamically creating widgets with unique names?

Any advice would be greatly appreciated

Answer

The solution is to store the widgets in a data structure such as a list or dictionary. For example:

entries = []
for num in range(quantity):usrInput = Entry(root, text = num)usrInput.pack()entries.append(usrInput)

Later, you can iterate over this list to get the values:

for entry in entries:value = entry.get()print("value: {}".format(value))

And, of course, you can access specific entries by number:

print("first item: {}".format(entries[0].get()))
https://en.xdnf.cn/q/118287.html

Related Q&A

Graphene-Django Filenaming Conventions

Im rebuilding a former Django REST API project as a GraphQL one. I now have queries & mutations working properly.Most of my learning came from looking at existing Graphene-Django & Graphene-Py…

Summing up CSV power plant data by technology and plant name

Ive got a question regarding the Form 860 data about US power plants.It is organized block-wise and not plant-wise. To become useful, the capacity numbers must be summed up.How may I get the total capa…

Send and receive signals from another class pyqt

I am needing a way to receive signals sent by a Class to another class. I have 2 classes: In my first class I have a function that emits a signal called asignal In my second class I call the first cla…

I can not add many values in one function

I have a gui applicationI put text into text box1, text box2,………… text box70 ,and then click on the pushButton, The function return_text () in the module_b.py be called. Now I can call one instance…

Close browser popup in Selenium Python

I am scraping a page using Selenium, Python. On opening the page one Popup appears. I want to close this popup anyway. I tried as below:url = https://shopping.rochebros.com/shop/categories/37browser = …

How can I replace certain string in a string in Python?

I am trying to write two procedures to replace matched strings in a string in python. And I have to write two procedures. def matched_case(old new): .........note: inputs are two strings, it returns a…

Python: `paste multiple (unknown) csvs together

What I am essentially looking for is the `paste command in bash, but in Python2. Suppose I have a csv file:a1,b1,c1,d1 a2,b2,c2,d2 a3,b3,c3,d3And another such:e1,f1 e2,f2 e3,f3I want to pull them toget…

Django Redirect after Login Error

Im new to Django and I know that to redirect after login I have to set the parameter page. But this only works when the login is successful. How can i do the same thing when some error occurs?? Ps: I…

Python: Pulling .png from a website, outputting to another

Hoping this question is not too vague, or asking for too much. Essentially I am analyzing large amounts of spectra, and wanting to create one large webpage that contains these spectra rather than looki…

Using peakutils - Detecting dull peaks

Im currently using peakutils to find peaks in some data. My data contains some "dull peaks", that is my peaks plateau somewhat. I cant set my code to find these peaks even after playing aroun…