Oct-21-2023, 08:38 PM
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
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
my problem is i get an error code of Key Error that follows
1 2 |
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 ) |
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
1 2 3 |
saved_info = [] for i in range ( 0 , self .count): city_name = globals ()[ f "city_ent{i}" ].get().title() |
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 class1 2 |
self .city_ent0 = Entry() self .city_ent0.grid(row = 1 , column = 1 ) |