-3

I am trying to get the first three characters of a Tkinter entry, and then add them to another tkinter entry.

For example:

name = Entry=(root, text="Name: ")

age = Entry=(root, text="Age: ")

username = First three characters of name + age

Then I want the first three letters of their name to add to the age to create a user name. If the user enters 'Taylor' as a 'name' and '13' as a 'age', I want to make a variable called 'username' which would contain 'Tay13'

Not sure if I made it too clear, but hopefully you understand. Thanks

EDIT: Just tried something else and it says 'StringVar' object is not subscriptable.

K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
  • It would be pretty insane to define a variable that dynamically, at least let the user enter that information using a button... – Nae Feb 01 '18 at 16:21
  • 1
    _"Just tried something else..."_ Why don't you go ahead and post that so you're not wasting other people's time who may be trying what you already tried? Post your code attempts - right now this question is low quality. – K.Dᴀᴠɪs Feb 01 '18 at 18:19

4 Answers4

1

Just found out how to do it on Reddit. If any one else need this, then here is the answer:

username = name.get()[:3] + age.get()

This gets the first 3 letters from 'name' and adds 'age' on to the end of it.

Thanks to the people who helped.

0

You can do this to get the username. The [:3] pulls the first 3 letters of a string.

username = str(name[:3]) + str(age)
print username
Zack Tarr
  • 851
  • 1
  • 8
  • 28
0

Below example adds/updates id to users dictionary each time the button is pressed:

try:                        # In order to be able to import tkinter for
    import tkinter as tk    # either in python 2 or in python 3
except ImportError:
    import Tkinter as tk


def update_users():
    global users, name, age
    user_id = name.get()[:3] + age.get()
    users[user_id] = None
    print(users)


if __name__ == '__main__':
    root = tk.Tk()
    users = dict()
    name = tk.Entry(root)
    age = tk.Entry(root)
    update_btn  = tk.Button(root, text="Update Users", command=update_users)
    name.pack()
    age.pack()
    update_btn.pack()
    root.mainloop()
Nae
  • 14,209
  • 7
  • 52
  • 79
0

This how to go about this create two entry widget to receive the content in the entry.Then your funtion to print the content in the entry and slice the first three letters of the name and print plus the age.

from tkinter import *


def Print_variable():
    e1 = name.get()
    e2 = age.get()
    print(e1[:3] + e2)


root = Tk()
root.geometry("400x400")

name = StringVar()  
e1 = Entry(root, textvariable=name)   # this entry accept the name
e1.pack()

age = StringVar()
e2 = Entry(root, textvariable=age)  # this entry accept the age
e2.pack()

b = Button(root, text="print name and age variable", command=Print_variable)
b.pack()

root.mainloop()
AD WAN
  • 1,414
  • 2
  • 15
  • 28