如何在变量末尾增加数字?

时间:2017-12-08 22:08:04

标签: python python-3.x tkinter

我有一个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_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]

中的数字

以下是我一直在尝试的但它不起作用:

    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.")

现在我得到一个“无法分配给函数调用”错误,但我甚至不确定我是否正确地进行此操作。

1 个答案:

答案 0 :(得分:2)

您可以将所有输出添加到列表中:

self.outputs = [self.output0, self.output1, self.output2] #etc...

然后循环遍历它们:

for output in self.outputs:
    output.delete(0, END) #etc...

您甚至可以创建如下输出:

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)

虽然您需要添加一种方法来更改行号以及每次迭代需要更改的其他内容。