Python Forum
Create an identification code from user input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create an identification code from user input
#1
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!
Reply
#2
use f-string and slices
example:
>>> year = 1945
>>> word = 'California'
>>> name = f"{word[:2]}{year}.txt"
>>> name
'Ca1945.txt'
>>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGUI] [Solved]Help storing in user input from line edit Extra 2 1,681 May-12-2022, 07:46 PM
Last Post: Extra
  Convert combobox user input in to date with tkinter Ame 8 6,662 Jul-01-2020, 09:40 PM
Last Post: Yoriz
  [PyQt] Create a GUI for alrady written code. francois072 5 2,622 Jun-04-2020, 09:55 PM
Last Post: deanhystad
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 13,670 Mar-20-2020, 07:21 AM
Last Post: deanhystad
  PyQt5: How do you set the user input of a line edit to a specific variable? YoshikageKira 17 11,478 Dec-26-2019, 03:18 PM
Last Post: Denni
  [Tkinter] Is there a way to sleep without stopping user input? GalaxyCoyote 2 2,100 Oct-23-2019, 06:23 PM
Last Post: Denni
  [Tkinter] help please, checking the user input and outputting the result tomkovladko 3 2,712 Mar-05-2019, 08:58 PM
Last Post: joe_momma
  [PyGUI] Hi All, how to hide/mask user input in PySimpleGUI nmrt 1 14,841 Sep-21-2018, 09:59 AM
Last Post: nmrt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020