Reputation: 1191
I have a Python/Tkinter program and I am trying to increment some numbers that are at the end of a variable used for tk.Entry boxes.
For example, self.output0, self.output1, self.output2, etc
(Sorry, my code is long so I tried shortening it up for an example.)
self.entries = []
self.output0 = tk.Entry(self, width=149, justify='center', bd='0', bg='#E0E0E0')
self.output0.grid(column=0, row=6, columnspan=5, padx=1)
self.output1 = tk.Entry(self, width=149, justify='center', bd='0', bg='#E0E0E0')
self.output1.grid(column=0, row=7, columnspan=5, padx=1)
I grab the users input with another entry box such as 'self.first_entry' and run a SQL query on the user input.
For example:
class Adder(ttk.Frame):
"""The adders gui and functions."""
def __init__(self, parent, *args, **kwargs):
ttk.Frame.__init__(self, parent, *args, **kwargs)
self.root = parent
self.init_gui()
def authenticate(self):
return pypyodbc.connect('Driver={SQL Server};Server=MYSERVERNAME;Database=MYDATABASENAME;Trusted_Connection=yes;')
def calculate(self):
firstname = str(self.first_entry.get()) # converts user entry into variable
lastname = str(self.last_entry.get())
license = str(self.lic_entry.get())
try:
connection = self.authenticate()
except pypyodbc.Error as ex:
sqlstate = ex.args[0]
if sqlstate == '28000':
self.output0.delete(0, END)
self.output0.insert(0,"You do not have access.")
cursor = connection.cursor()
if (firstname and not lastname and not license): # "You entered first name."
SQLCommand = ("SELECT LASTNAME, FIRSTNAME, LICNUMBER "
"FROM MyTABLEName " # table name
"with (nolock)"
"WHERE FIRSTNAME LIKE ?")
Values = [firstname + '%']
else:
SQLCommand = ("SELECT LASTNAME, FIRSTNAME, LICNUMBER "
"FROM MyTABLEName " # table name
"(nolock)"
"WHERE FIRSTNAME LIKE ? AND LASTNAME LIKE ? AND LICNUMBER LIKE ?")
Values = [firstname + '%', lastname + '%', '%' + license + '%']
cursor.execute(SQLCommand,Values)
results = cursor.fetchmany(10)
if results:
self.output0.delete(0, END) # clears entry
self.output0.insert(0,results[0]) # enters results in entry
self.output1.delete(0, END) # clears entry
self.output1.insert(0,results[1]) # enters results in entry
self.output2.delete(0, END) # clears entry
self.output2.insert(0,results[2]) # enters results in entry
self.output3.delete(0, END) # clears entry
self.output3.insert(0,results[3]) # enters results in entry
self.output4.delete(0, END) # clears entry
self.output4.insert(0,results[4]) # enters results in entry
connection.close()
else:
self.output0.delete(0, END)
self.output0.insert(0,"There are no records for this input.")
The above will output 5 rows from the database.
I am wanting to shorten the code up. How can I take the below and write it so that I do not have to repeat
self.output0.delete(0, END) # clears entry
self.output0.insert(0,results[0]) # enters results in entry
self.output1.delete(0, END) # clears entry
self.output1.insert(0,results[1]) # enters results in entry
2
2
3
3
...etc
I want to increment the outputX and the number in results[x]
This below is what I have been trying but it is not working:
results = cursor.fetchmany(10)
i = 0
myDelete = (self.output%d.delete(0, END) % (i))
myInsert = (sef.output%d.insert(0,results[0]) % (i))
if results:
for int(i) in myDelete, myInsert (i):
i += 0
print (myDelete) # clears entry
print (myInsert) # enters results in entry
connection.close()
else:
self.output0.delete(0, END)
self.output0.insert(0,"There are no records for this input.")
Right now I am getting a "Can't assign to function call" error but I am not even sure if I am going about this the right way.
Upvotes: 0
Views: 97
Reputation: 153
You could add all the outputs to a list:
self.outputs = [self.output0, self.output1, self.output2] #etc...
And then loop through them:
for output in self.outputs:
output.delete(0, END) #etc...
You can even create outputs like such:
self.outputs = []
for i in range(4):
output = tk.Entry(
self, width=149, justify='center', bd='0', bg='#E0E0E0'
)
output.grid(column=0, row=6, columnspan=5, padx=1)
self.outputs.append(output)
Though you would need to add a way to change the row number and whatever else needs to be different for each iteration.
Upvotes: 2