我不能,或者不理解如何使用列表来解决这个问题,因为我希望能够在单独的函数中收集创建的变量的值。这些变量保存的数据可以由用户随时更新。下面是我的代码,它应该显示我想要做的事情。
def Customer_Edit():
CustomerEdit = Tk()
global customerdetails2
customerdetails2 = db.execute('''SELECT CUSTOMER_ID, FORENAME, SURNAME, DOB, GENDER, ADDRESS, EMAIL, TELEPHONE, BAND_NAME FROM CUSTOMER''')
AA01 = Label(CustomerEdit, text = "Customer ID")
AA01.grid(row=0, column=0)
AA02 = Label(CustomerEdit, text = "Forename")
AA02.grid(row=0, column=1)
AA03 = Label(CustomerEdit, text = "Surname")
AA03.grid(row=0, column=2)
AA04 = Label(CustomerEdit, text = "DOB")
AA04.grid(row=0, column=3)
AA05 = Label(CustomerEdit, text = "Gender")
AA05.grid(row=0, column=4)
AA06 = Label(CustomerEdit, text = "Address")
AA06.grid(row=0, column=5)
AA07 = Label(CustomerEdit, text = "E-Mail")
AA07.grid(row=0, column=6)
AA08 = Label(CustomerEdit, text = "Telephone")
AA08.grid(row=0, column=7)
AA09 = Label(CustomerEdit, text = "Band Name")
AA09.grid(row=0, column=8)
editnum = 1
global CustomerDetailsEntries
CustomerDetailsEntries = []
for index, row in enumerate(customerdetails2):
global entry
entry = Entry(CustomerEdit)
entry.insert(0, row[0])
entry.grid(row = editnum, column = 0)
CustomerDetailsEntries.append(entry.get())
global entry1
entry1 = Entry(CustomerEdit)
entry1.insert(0, row[1])
entry1.grid(row = editnum, column = 1)
CustomerDetailsEntries.append(entry1.get())
global entry2
entry2 = Entry(CustomerEdit)
entry2.insert(0, row[2])
entry2.grid(row = editnum, column = 2)
CustomerDetailsEntries.append(entry2.get())
global entry3
entry3 = Entry(CustomerEdit)
entry3.insert(0, row[3])
entry3.grid(row = editnum, column = 3)
CustomerDetailsEntries.append(entry3.get())
global entry4
entry4 = Entry(CustomerEdit)
entry4.insert(0, row[4])
entry4.grid(row = editnum, column = 4)
CustomerDetailsEntries.append(entry4.get())
global entry5
entry5 = Entry(CustomerEdit)
entry5.insert(0, row[5])
entry5.grid(row = editnum, column = 5)
CustomerDetailsEntries.append(entry5.get())
global entry6
entry6 = Entry(CustomerEdit)
entry6.insert(0, row[6])
entry6.grid(row = editnum, column = 6)
CustomerDetailsEntries.append(entry6.get())
global entry7
entry7 = Entry(CustomerEdit)
entry7.insert(0, row[7])
entry7.grid(row = editnum, column = 7)
CustomerDetailsEntries.append(entry7.get())
global entry8
entry8 = Entry(CustomerEdit)
entry8.insert(0, row[8])
entry8.grid(row = editnum, column = 8)
CustomerDetailsEntries.append(entry8.get())
print (CustomerDetailsEntries)
editnum = editnum + 1
finaleditnum = editnum + 1
AA10 = tkinter.Button(CustomerEdit, text = "Save Changes", command = SaveChanges)
AA10.grid(row = finaleditnum, column = 4)
def SaveChanges():
UpdatedEntry = entry.get()
UpdatedEntry1 = entry1.get()
UpdatedEntry2 = entry2.get()
UpdatedEntry3 = entry3.get()
UpdatedEntry4 = entry4.get()
UpdatedEntry5 = entry5.get()
UpdatedEntry6 = entry6.get()
UpdatedEntry7 = entry7.get()
UpdatedEntry8 = entry8.get()
现在代码相对不完整,但目前,程序从数据库中收集数据,定义为变量' customerdetails2'并将所有数据输入到输入字段中,并为程序从中收集数据的表中的每一行数据创建一个新行。当按钮“保存更改”时单击,我希望程序从最新的输入字段中收集所有数据并更新数据库。
SaveChanges()是我想要从创建的输入字段中收集所有数据以更新数据库的地方。但是我知道这是不可能的,因为变量会针对数据库中的每一行进行更新,因此程序只会从最后一行条目中收集数据。
我的问题是有没有办法让Python自动为数据库中的每一行创建唯一命名的变量&user;数据库2'这样就可以收集来自每个单一输入字段的数据,因此保存到数据库中?这可能吗?
任何帮助都会非常感激,因为我一直坚持这个问题。我希望我已经清楚地解释了为什么在我所知道的情况下使用列表来做我想做的事情并不是一个选择。
答案 0 :(得分:4)
您可以使用字典存储Tkinter小部件并稍后访问它们以获取信息。
创建小部件:
customer_info = {'name': None,
'city': None,
'email': None,
'phone': None,
'alias': None}
for i, each in enumerate(customer_info):
customer_info[each] = Entry(CustomerEdit)
customer_info[each].insert(0, customerdetails2[i])
customer_info[each].grid(row = editnum, column = i)
现在你有了一个字典,customer_info['name']
是Entry Widget,用户可以输入他的名字。因此,您可以使用customer_info['name'].get()
来检索输入。
稍后阅读信息:
for key, val in customer_info.iteritems():
print val.get()
这是一个完全独立的工作示例...我假设customerdetails2
是一个列表列表,根据您的代码我相信它是。我注释掉了你的dbquery并添加了我自己的结果。我添加了一些东西让Tkinter打开,所以你可能需要根据它进行编辑:
import Tkinter as Tk
from Tkinter import *
from collections import OrderedDict
def Customer_Edit(root):
CustomerEdit = Frame(root)
global customerdetails2
global customer_dict
#customerdetails2 = db.execute('''SELECT CUSTOMER_ID, FORENAME, SURNAME, DOB, GENDER, ADDRESS, EMAIL, TELEPHONE, BAND_NAME FROM CUSTOMER''')
customerdetails2 = [['1234', 'Foo', 'Bar', '01/01/01', 'M', 'Foo Street', 'foo@bar.com', '1800foo', 'foobarband'], ['4444', 'Sally', 'Bear', '12/12/12', 'F', 'Sally Street', 'Sally@bar.com', '1800Sally', 'Sally Band']]
customer_info = OrderedDict([('Customer ID', None),
('Forname', None),
('Surname', None),
('DOB', None),
('Gender', None),
('Address', None),
('E-Mail', None),
('Telephone', None),
('Band', None)])
customer_dict = {}
for i, customer in enumerate(customerdetails2):
for r, each in enumerate(customer_info):
if i == 0:
Label(CustomerEdit, text=each).grid(row= 0, column= r)
customer_info[each] = Entry(CustomerEdit)
customer_info[each].insert(0, customer[r])
customer_info[each].grid(row= i+1, column= r)
customer_dict[i] = dict(customer_info)
AA10 = Button(CustomerEdit, text = "Save Changes", command = SaveChanges)
AA10.grid(row=len(customer_dict) + 1, column= 4)
CustomerEdit.pack()
def SaveChanges():
for k, v in customer_dict.iteritems():
print 'Customer Number: ' + str(k)
for title, info in v.iteritems():
print '{} - {}'.format(title, info.get())
root = Tk()
Customer_Edit(root)
root.mainloop()