【问题标题】:Python and Tkinter GUI - How to Add Id Automatically and Increment by 1 After Each Submission?Python 和 Tkinter GUI - 如何在每次提交后自动添加 Id 并递增 1?
【发布时间】:2021-09-18 20:00:20
【问题描述】:

我想制作一个用户输入客户信息的 GUI,但我想为每个客户添加一个 ID,以便在搜索该 ID 时可以找到该客户。每次提交后,id要加一。

例如,第一个客户的 id 应该是 CUS1001,当我提交客户时,第二个客户的 id 应该是 CUS1002。由于无法追踪客户数量,因此无法手动输入id。

我到目前为止所做的代码附在下面。如果您能帮我解决这个问题,我将不胜感激。

from tkinter import *
import tkinter.simpledialog
import csv
from tkinter import messagebox

def store_member():

afirstname = firstname.get()
asurname = surname.get()
aemail = email.get()
aemailaddress = emailaddress.get()
fullemail = aemail + aemailaddress
print(fullemail)
aday = day.get()
amonth = month.get()
ayear = year.get()
date_of_birth = aday + "." + amonth + "." + ayear
print(date_of_birth)

agender_choice = gender_choice.get()
amobile = mobile.get()
fullmobile = "44" + amobile

if afirstname == "" or asurname == "" or aemail == "" or amobile == "":
    print('Error')
    messagebox.showerror("error", "there was an error")
    firstname.set("")
    surname.set("")
    email.set("")
    mobile.set("")

elif agender_choice > 2:
    print('Error 2')
    messagebox.showerror("error" , "please select a gender")
    firstname.set("")
    surname.set("")
    email.set("")
    mobile.set("")

elif(len(fullmobile)!=12):
    print( 'Error 3')
    messagebox.showerror("error" , "the mobile prone numebr is incomplete")
    firstname.set("")
    surname.set("")
    email.set("")
    mobile.set("")

else:
    result = messagebox.askquestion("Submit" , "You are about the enter the data\n" + afirstname + "\n" + asurname +"\n" + fullemail + "\n" )

    if(result == 'yes'):
        print('here')

        with open('customer.txt' , 'a') as csvfile:
            writer = csv.writer(csvfile)
            writer.writerow([afirstname,asurname,fullemail,date_of_birth,agender_choice,amobile])
        csvfile.close()
    else:
        firstname.set("")
        surname.set("")
        email.set("")
        mobile.set("")

def back():
    print('here')

def clear_enter_screen():
    firstname.set("")
    surname.set("")
    email.set("")
    emailaddress.set("@hotmail.com")
    day.set("1")
    month.set("January")
    year.set("2018")
    mobile.set("")

def help_entry():
    messagebox.showinfo("Information" , "Enter all data fully")

root = Tk()
root.config(background = 'light grey')

#defining all variables for each gui component
firstname = StringVar()
surname = StringVar()
email = StringVar()
emailaddress = StringVar()
day = StringVar()
month = StringVar()
year = StringVar()
gender_choice = IntVar()
mobile = StringVar()

#gui components for each row
#ROW 0
lblHeading = Label(root, text = "New Customer" , font = ('Arial' , 24 , "bold")).grid(row = 0, column = 0, columnspan = 4, padx=20, pady=10)

#row1
lblFirstName = Label(root, text = "First Name" , font= ('Arial', 14, "bold")).grid(row=1, column=1)
entryFirstName = Entry(root, textvariable = firstname, width = 10). grid(row=1, column=2)

#row 2
lblLastName = Label(root, text = "Last Name" , font = ('Arial' , 14, "bold")).grid(row = 2, column =1)
entryLastName = Entry(root, textvariable = surname, width = 10).grid(row=2 , column=2)

# row 3
lblGender = Label(root, text = "Gender" , font = ('Arial', 14, "bold")).grid(row=3, column=0)
radiobutton1 = Radiobutton(root, text = "Male", variable= gender_choice, value=1, font=('Arial', 14, "bold")).grid(row=3, column=1)
radiobutton2 = Radiobutton(root, text = "Female", variable=gender_choice,value=2, font=('Arial',14,"bold")).grid(row=3, column=2)

#row4
lblemail = Label(root,text ="Email", font=('Arial',14, "bold")).grid(row=4, column = 0)

#entry box
entryemail = Entry(root, textvariable = email, width = 10).grid(row=4, column=1)
#drop down list
list1 = ['@hotmail.com', '@gmail.com' , '@yahoo.com']
droplist = OptionMenu(root, emailaddress, *list1)
droplist.config(width = 15)
emailaddress.set('@hotmail.com')
droplist.grid(row=4, column=2)

#row5
lblbirthday = Label(root, text = 'birthday' , font=('Arial' , 14, 'bold')).grid(row=5 , column=0)
list2 = ['1', '2', '3' , '4' , '5']
droplist = OptionMenu(root,day,*list2)
droplist.config(width=5)
day.set('1')
droplist.grid(row=5, column=1)

list3 = ['January', 'February', 'March']
droplist = OptionMenu(root,month,*list3)
droplist.config(width=15)
month.set('January')
droplist.grid(row=5, column=2)

list4 = ['2021', '2022', '2023']
droplist = OptionMenu(root, year, *list4)
droplist.config(width=8)
year.set('2021')
droplist.grid(row=5, column=3)

#row6 components
lblmobile= Label(root, text= "mobile +44" , font=('Arial' , 14, "bold")).grid(row=6, column=1)
entrymobile = Entry(root, textvariable = mobile, width=10).grid(row=6, column=2)

#row7 buttons
submit_button = Button(root,text = "Store", command=store_member , bg='orange', fg='white').grid(row=7, column=0, sticky=E, padx=20, pady=18)
root.mainloop()

【问题讨论】:

  • 建议使用支持自增字段的sqlite3数据库来存储客户记录。
  • 您好,感谢您的建议,但我想在 csv 文件上执行此操作。

标签: python user-interface tkinter form-submit


【解决方案1】:

如果您使用的是 csv,请使用此方法:

先创建“id.txt”然后

添加功能

def customer_id():
    with open ("id.txt", 'r') as file:
        pre_id=file.read()
    new_id="CUS"+str(int(pre_id[3:])+1)
    with open("id.txt",'w') as file:
        file.write(new_id)
    return new_id



#warning: for the above function the prefix should be CUS(or any 3 letter word)

#note: I can't achieve id as "CUS0001" the above function give id as "CUS1"

【讨论】:

  • 非常感谢。我修改了你的想法。我已经将第一个客户手动写入 csv,id 为 1。然后,在每次提交新客户之前,我取最后一行的第一个值并递增 1。之后将单词组合成一个字符串。
  • 欢迎,你的想法比我的好。
猜你喜欢
  • 2019-04-28
  • 1970-01-01
  • 1970-01-01
  • 2017-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-31
  • 2020-11-05
相关资源
最近更新 更多