1

I wanted to write a small program in Python 3.4 where I have to enter a full name like "Gumbo Froehn" and the program splits it into name and surname. However, I have trouble reading the entry field.

Here is my code (so far):

from tkinter import *                   
from tkinter import ttk                 


class FullScreenApp(object):                        # Klasse fuer die Vollbild Darstellung
    def __init__(self, master, **kwargs):
        self.master=master
        pad=3
        self._geom='200x200+0+0'
        master.geometry("{0}x{1}+0+0".format(
            master.winfo_screenwidth()-pad, master.winfo_screenheight()-pad))
        master.bind('<Escape>',self.toggle_geom)
    def toggle_geom(self,event):
        geom=self.master.winfo_geometry()
        print(geom,self._geom)
        self.master.geometry(self._geom)
        self._geom=geom


mainWindow = Tk()                                   # Generiert das Hauptfenster aus tkinter
app=FullScreenApp(mainWindow)                       # Ruft Klasse fuer Vollbild-Darstellung auf
mainWindow.title("Mark's Latex Code Generator")     # Definiert den fenster-Titel



fullname = StringVar()
LBLeingabename = Label(mainWindow, text="Write full name:").place(x=20, y=350)
EFvollername = Entry(mainWindow,textvariable=str(fullname), width=50)
EFvollername.place(x=200,y=350)

splitname = fullname.get().split()


LBtestausgabe = Label(mainWindow, text="Testausgabe").place(x=20, y=770)
TFtestausgabe = Text(mainWindow, height=5,width=50)
TFtestausgabe.place(x=20,y=800)



def test():
    TFtestausgabe.insert(END, EFvollername.get()+"\n")
    TFtestausgabe.insert(END, splitname.get()+"\n")
    TFtestausgabe.insert(END, type(fullname))

Btn = ttk.Button(mainWindow, text="OK",command=test).place(x=450,y=800)

mainloop()  # Haelt das Programm offen bis es beendet wird

When I run the program, only the line TFtestausgabe.insert(END, EFvollername.get()+"\n") is working, giving me the full name.

I want to use the StringVar variable fullname for the string-split operation. But this doesn't work yet. When I use .set() to enter a predefined name into the fullname variable it works.

What am I doing wrong? Do I have some of the code lines in the wrong order?

CubeJockey
  • 2,209
  • 8
  • 24
  • 31
Cpt. Hirni
  • 37
  • 1
  • 7

1 Answers1

0
EFvollername = Entry(mainWindow,textvariable=str(fullname), width=50)

Don't convert the textvariable to a string. Leave it as a StringVar:

EFvollername = Entry(mainWindow,textvariable=fullname, width=50)

You also need to move splitname = fullname.get().split() so that it is inside test. As it is, you're getting the value a fraction of a second after the text box is created, so the user has no time to actually type anything in before you get the value.

Also, splitname.get() won't work because splitname is a list and lists don't have a get method. I'm not exactly sure what you're trying to do on that line, but maybe you want TFtestausgabe.insert(END, str(splitname)+"\n")?


Incidentally, you should not use the place method on the same line as you assign a widget to a variable. Since place returns None, that's the value your variable will have. See Why do my Tkinter widgets get stored as None? for more information.

Community
  • 1
  • 1
Kevin
  • 74,910
  • 12
  • 133
  • 166
  • Hi Kevin! Thanks a lot!! Now it works like I want it. And I have learned a bit more how to structure the code. Thank you very much! – Cpt. Hirni Aug 11 '15 at 08:50