Python Forum
dynamic variable name declaration in OOP style project problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dynamic variable name declaration in OOP style project problem
#1
Ok so basically im trying to build a flight tracker app. And i have a problem with the gui py file. I have a button that generates variable names using fstrings. an example follows


globals()[f'city_lbl{self.count}'] = Label(text="City to fly to : ")
globals()[f"city_lbl{self.count}"].grid(row=self.cur_row, column=0)
i want to give the user the ability to enter multiple destination names and create a dataframe on another py file that includes time the trip can start and by when you have to be back, as well as, generate IATA codes for each supplied city. I want to be able to save this dataframe in a csv file that can also be read using a different button.

my problem arises when i try to extract all of the city names supplied by the user i create a for loop with the range function (maximum being the self.count variable) and i append the resulting value in a list for further examination. so i have the following code



saved_info=[]
for i in range(0,self.count):
    city_name=globals()[f"city_ent{i}"].get().title()
my problem is i get an error code of Key Error that follows

Error:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\giorg\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ File "C:\Users\giorg\PycharmProjects\pythonProject2\day_39_udemy\gui.py", line 59, in save_ent if globals()[f"city_ent{i}"].get(): ~~~~~~~~~^^^^^^^^^^^^^^^^ KeyError: 'city_ent0'
im guessing this means that it cant find the assigned entry even though i have generated that entry with the following code. now if i was to guess is that because the assigned entry is using self. before the declaration. i have tried putting self. in front of the dynamic declaration (that uses globals() and uses a key to generate the variable name) but it wouldn't work. can someone help me? I want to make this app OOP style that contain py files with classes on each but in order to access the parameters like variable values and such self. has to be used otherwise it is not accessible past the initialization phase of the class


self.city_ent0 = Entry()
self.city_ent0.grid(row=1, column=1)
Reply
#2
I have yet to see code where dynamically creating variables was a good idea. Why make variables named city_lbl1, city_lbl2 instead of making a list city_lbl? Having a list makes it really easy to loop through all the names.
Reply
#3
(Oct-21-2023, 10:25 PM)deanhystad Wrote: I have yet to see code where dynamically creating variables was a good idea. Why make variables named city_lbl1, city_lbl2 instead of making a list city_lbl? Having a list makes it really easy to loop through all the names.
how would i go about doing that? im making dynamic variables because i want to keep adding columns of information. i really dont know how else to do it
Reply
#4
Post what you have done so far. Your previous examples do not show why you would need dynamic variables.

Looking at your previous code I would do something like this:
import tkinter as tk


class Form(tk.Tk):
    def __init__(self, fields):
        super().__init__()
        self.fields = fields
        for row, (field, text) in enumerate(fields.items()):
            tk.Label(self, text=text).grid(row=row, column=0, sticky="e")
            entry = tk.Entry(self, width=30)
            entry.grid(row=row, column=1, sticky="w")
            self.fields[field] = entry

        tk.Button(
            self, text="Print Fields", command=self.print_fields
        ).grid(row=row+1, column=0, columnspan=2, sticky="ew")

    def print_fields(self):
        for name, entry in self.fields.items():
            print(name, entry.get())


fields = {
    "departure": "Leaving from city",
    "destination": "City to fly to",
}
Form(fields).mainloop()
That might be way off base, but it is how I would make a window where the widgets are defined by something outside the window's code. Essentially I am creating dynamic variables in the class scope, but I control what widgets are in that namespace. Everything in self.fields will be an entry widget. I can loop over the widgets by looping over the values in the dictionary.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with importing python-telegram library into the project gandonio 1 1,580 Nov-01-2022, 02:19 AM
Last Post: deanhystad
  Variable declaration Asraful_Islam 5 3,048 Mar-25-2021, 05:31 PM
Last Post: nilamo
  Reset Variable problem IcodeUser8 3 2,366 Jul-20-2020, 12:20 PM
Last Post: IcodeUser8
  Alternative to dynamic variable names catosp 13 4,603 Jun-20-2020, 03:45 PM
Last Post: catosp
  Use dynamic variable from parallel running python script Sicksym 0 1,854 May-15-2020, 02:52 PM
Last Post: Sicksym
  Variable from notepad problem samuelbachorik 2 2,306 Apr-10-2020, 09:04 AM
Last Post: samuelbachorik
  Problem: Get variable JohnnyCoffee 0 1,605 Feb-22-2020, 09:26 PM
Last Post: JohnnyCoffee
  Problem defining a variable rix 6 3,207 Dec-17-2019, 11:34 AM
Last Post: rix
  Problem writing a variable value to Excel Chuck_Norwich 1 1,947 Jul-25-2019, 02:20 PM
Last Post: Chuck_Norwich
  3rd problem from project Euler Richard_SS 6 3,205 Jun-05-2019, 02:06 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020