简体   繁体   中英

How to automatically create variables WITHOUT using a list

I cannot , or do not understand how to use a list for this problem because I am looking to be able to gather the values of the created variables in a seperate function. The data held by these variables can be updated at any time by the user. Below is my code which should show what I am trying to do.

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()

The code is relatively incomplete right now but at the moment, the program gathers data from the database, defined as the variable 'customerdetails2' and enters all the data into entry fields and a new row is created for each row of data in the table that the program is gathering data from. When the button 'Save Changes' is clicked, I want the program to gather all data from the up-to-date entry fields and update the database.

SaveChanges() is where I am looking to gather all data from the created entry fields in order to update the database. However I am aware that this is not possible as the variables get updated for each row that is in the database so the program will only gather the data from the very last row of entries.

My question is is there a way to make Python automatically create uniquely named variables for each row in the database 'customerdatabase2', which would make it possible for data from every single entry field to be gathered and therefore saved into the database? Is this possible?

Any help would be greatly appreciated as I have been stuck on this problem for a while. I hope I have explained clearly why using a list for what I am trying to do is not an option as far as I can tell.

You can use a dictionary to store the Tkinter widgets and access them later to get the information.

Creating the widgets:

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)

You now have a dictionary such that customer_info['name'] is the Entry Widget where the user can input his name. So you can then use customer_info['name'].get() to retreive the input.

Reading the info later:

for key, val in customer_info.iteritems():
    print val.get()

Here is a fully standalone working example... I am assume that customerdetails2 is a list of lists, based on your code I believe that it is. I commented out your dbquery and added in my own results. I added in a few things to get Tkinter to open so you may need to edit based on that:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM