Mikey
Mikey

Reputation: 149

Python Tk Label Error with StringVar

I'm going to create a program that resembles the image below. The interface uses one text entry for a name, one button, and two labels. The button should have the text Say hello and when the user clicks the button, the bottom label should display the name with Hi in front of it (see image below)

Here's what I've got

from tkinter import *
from tkinter.ttk import *


def say_hello():
    name_var.set(name_entry.get())

def main():

    global window, name_var, name_entry
    window = Tk()

    top_label = Label(window, text='Enter a name below')
    top_label.grid(row=0, column=0) 

    name_var = StringVar()

    name_entry = Entry(window, textvariable=name_var)
    name_entry.grid(row=1, column=0)

    hello_button = Button(window, text='Say hello', command=say_hello)
    hello_button.grid(row=2, column=0)

    bottom_label = Label(window, text='Hi ' + name_var)
    bottom_label.grid(row=3, column=0)   

    window.mainloop()

main()

When I try to run it I get this error:

Traceback (most recent call last): File "C:\Program Files (x86)\Wing IDE 101 5.1\src\debug\tserver_sandbox.py", line 29, in <module> File "C:\Program Files (x86)\Wing IDE 101 5.1\src\debug\tserver_sandbox.py", line 24, in main builtins.TypeError: Can't convert 'StringVar' object to str implicitly

Everything works GUI wise, I'm just not sure how to get the last label that says "Hi Jack" to come up after pressing the button — i.e what my command should be in the hello_button line.

Upvotes: 0

Views: 1428

Answers (3)

Padraic Cunningham
Padraic Cunningham

Reputation: 180471

This simple class should do what you want:

from tkinter import Button, Tk, Entry,StringVar,Label


class App():
    def __init__(self, **kw):
        self.root = Tk()
        # hold value for our output Label
        self.s = StringVar() 
        # label above our Entry widget 
        self.l = Label(self.root, text="Enter name here").pack()
        # will take user input
        self.e = Entry(self.root)
        self.e.pack()
        self.b = Button(self.root, text="Say Hello",command=self.on_click).pack()
        # textvariable points to our StringVar
        self.l2 = Label(self.root, textvariable=self.s).pack()
        self.root.mainloop()

     # every time the Button is pressed we get here
     # an update the StringVar with the text entered in the Entry box
    def on_click(self):
        self.s.set(self.e.get())


App()

You just need to create a couple of Labels, and Entry widget to take the name and a callback function to update the StringVar value so the label/name value gets updated.

Upvotes: 0

pzp
pzp

Reputation: 6607

Here's how I did it:

#!/usr/bin/env python2.7

import Tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        self.name_var = tk.StringVar()

        tk.Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

    def createWidgets(self):
        self.top_label = tk.Label(self, text='Enter a name below')
        self.top_label.grid(row=0, column=0)

        self.name_entry = tk.Entry(self)
        self.name_entry.grid(row=1, column=0)

        self.hello_button = tk.Button(self, text='Say hello', command=self.say_hello)
        self.hello_button.grid(row=2, column=0)

        self.output = tk.Label(self, textvariable=self.name_var)
        self.output.grid(row=3, column=0)

    def say_hello(self):
        self.name_var.set("Hi {}".format(self.name_entry.get()))

root = tk.Tk()
app = Application(master=root)
app.mainloop()

Ultimately it was very similar to your code. The only thing you were missing was how to use Tkinter.StringVar() correctly. You need to set the bottom label's textvariable to name_var when you create it, and then you should be good to go.

Upvotes: 0

Zizouz212
Zizouz212

Reputation: 4998

Here's your offensive code:

bottom_label = Label(window, text='Hi ' + name_var)

You can't really add a string and an instance of a class. A Tkinter StringVar isn't actually a string, but like a special thing for the gui to hold a string. That's why it can update automatically and stuff like that. Solution is simple:

bottom_label = Label(window, text = 'Hi ' + name_var.get())

Upvotes: 2

Related Questions