Python Forum
Create an identification code from user input - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Create an identification code from user input (/thread-25759.html)



Create an identification code from user input - PeroPuri - Apr-11-2020

Hi everyone,
I'm new in using python and tkinter and I'm stuck with my script, this one:
from openpyxl import *
from tkinter import *


wb = load_workbook('C:\\Users\\Me\\Desktop\\Script\\try.xlsx') 

sheet = wb.active 


def excel(): 

    sheet.cell(row=1, column=1).value = "name"
    sheet.cell(row=1, column=2).value = "birth"
    sheet.cell(row=1, column=3).value = "gender"
    sheet.cell(row=1, column=4).value = "mobile"



def focus1(event): 
    birth_field.focus_set() 


def focus2(event): 
    gender_field.focus_set() 


def focus3(event): 
    mobile_field.focus_set() 


def clear():    
    name_field.delete(0, END) 
    birth_field.delete(0, END) 
    gender_field.delete(0, END)
    mobile_field.delete(0, END)

def insert(): 
    if (fame_field.get() == "" and
        birth_field.get() == "" and
        gender_field.get() == "" and
        mobile_field.get() == ""):

        print("empty input") 

    else: 
        current_row = sheet.max_row 
        current_column = sheet.max_column 

        sheet.cell(row=current_row + 1, column=1).value = name_field.get() 
        sheet.cell(row=current_row + 1, column=2).value = birth_field.get() 
        sheet.cell(row=current_row + 1, column=3).value = gender_field.get() 
        sheet.cell(row=current_row + 1, column=4).value = mobile_field.get() 


        wb.save('C:\\Users\\Me\\Desktop\\Script\\try.xlsx') 

        name_field.focus_set() 

        clear() 


if __name__ == "__main__": 

 root = Tk() 

root.configure(background='light grey') 

root.geometry("500x300") 

excel()
#label
name = Label(root, text="name", bg="light grey") 

birthdate = Label(root, text="birthdate", bg="light grey")

gender = Label(root, text="gender", bg="light grey")

mobile = Label(root, text="mobile contact", bg="light grey") 

#grid
name.grid(row=1, column=0, sticky=W) 
birthdate.grid(row=2, column=0, sticky=W) 
gender.grid(row=3, column=0, sticky=W) 
mobile.grid(row=4, column=0, sticky=W) 

#entry
name_entry = Entry(root) 
birthdate_entry = Entry(root) 
gender_entry = StringVar()
mobile_entry = Entry(root) 


name_entry.bind("<Return>", focus1) 


birthdate_entry.bind("<Return>", focus2) 


gender_entry.bind("<Return>", focus4) 


mobile_entry.bind("<Return>", focus5) 


name_entry.grid(row=1, column=1, ipadx="100") 
birthdate_entry.grid(row=2, column=1, ipadx="100") 
gender_entry.grid(row=3, column=1, ipadx="100") 
mobile_entry.grid(row=4, column=1, ipadx="100") 


excel() 

submit = Button(root, text="Submit", fg="White", 
                            bg="Red", command=insert) 
submit.grid(row=12, column=1) 

root.mainloop()
What I need is to create a code from the user input like this: first two letter of the name, plus year of birth, age (that I need to calculate from the birthdate), gender and last two digits of mobile phone all separete with a "_" (e,g. na_2000_20_m_99). I'd like it to appear in a text wedge that the user can see but not edit and then save it in my excel file. Is it possible? Also my first choice for the gender option was to use Radiobutton but for some reason it keep gives me error and so I kept the text form. If somebody could help with my problems I will be very grateful. Thank you all and have a nice day!


RE: Create an identification code from user input - Larz60+ - Apr-11-2020

use f-string and slices
example:
>>> year = 1945
>>> word = 'California'
>>> name = f"{word[:2]}{year}.txt"
>>> name
'Ca1945.txt'
>>>