我有一个Python / Tkinter程序,我试图增加一些数字,这些数字位于用于tk.Entry框的变量的末尾。
例如,self.output0,self.output1,self.output2等
(对不起,我的代码很长,所以我试着缩短它的例子。)
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)
我使用另一个输入框抓取用户输入,例如&self; first_first_entry'并在用户输入上运行SQL查询。
例如:
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.")
以上将从数据库中输出5行。
我想缩短代码。我怎样才能采取以下内容并将其写下来,以便我不必重复
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
我想增加outputX和结果[x]
中的数字以下是我一直在尝试的但它不起作用:
cursor.execute(SQLCommand,Values)
results = cursor.fetchmany(10)
self.outputs = [self.output0, self.output1, self.output2, self.output3, self.output4]
for output in self.outputs:
for i in range(0, 4):
if results:
output.delete(0, END) # clears entry
output.insert(0,results[i]) # enters results in entry
else:
self.output0.delete(0, END)
self.output0.insert(0,"There are no records for this input.")
如何增加"结果[x]"中的数字?这样我从SQL查询中获得了不同的行?它在每一行上保持打印相同的行。
答案 0 :(得分:0)
问题是您正在尝试索引光标。尝试先将其转换为列表,然后运行该功能。
results = list(cursor.fetchmany(10))
答案 1 :(得分:0)
一个简单且不那么具有侵入性的解决方案是使用getattr
:
def update_fields(self, prefix, values):
for x, _ in enumerate(values):
attr = getattr(self, prefix + str(x))
attr.delete(0, END)
attr.insert(0, values[x])
然后你就是:
results = cursor.fetchmany(10)
if results:
self.update_fields('output', results)
请注意,getattr
如果AttributeError
没有名为self
的声明字段<{p}},则会引发outputX
您可以通过吞下异常来忽略未声明的字段:
def update_fields(self, prefix, values):
for x, _ in enumerate(values):
try:
attr = getattr(self, prefix + str(x))
attr.delete(0, END)
attr.insert(0, values[x])
except AttributeError:
pass
# You can also use continue