2

I'm making a program using Tkinter where the user inputs their weight in Pound and then it outputs their weight in kilo.

I'm having problems getting the contents of the Entry from the user. I'm calculating the pound to kilo in clicked1.

Can someone show me how I would get the Entry input there?

from Tkinter import *
import tkMessageBox

class App(object):
    def __init__(self):
        self.root = Tk()
        self.root.wm_title("Question 7")
        self.label = Label (self.root, text= "Enter your weight in pounds.")
        self.label.pack()

        self.entrytext = StringVar()
        Entry(self.root, textvariable=self.entrytext).pack()

        self.buttontext = StringVar()
        self.buttontext.set("Calculate")
        Button(self.root, textvariable=self.buttontext, command=self.clicked1).pack()

        self.label = Label (self.root, text="")
        self.label.pack()

        self.root.mainloop()

    def clicked1(self):
        input = 3423 #I would like the user input here.
        self.label.configure(text=input)

    def button_click(self, e):
        pass

App()

3 Answers 3

5

What you are looking for is [widget].get()

Text widget

In case you use the Text widget, you have to use [widget].get(1.0, END) where 1.0 means "first line, 0th character"

Code review

I've noticed a few other things in your code that could get improved:

  • PEP8 conformity; see pep8online.com
  • If you add a Shebang, Linux users will be able to execute it directly with ./script.py.
  • Variable naming:
    • input is a built-in function and you should avoid overwriting it
    • Use meaningful variable names (entrytext might be problematic in case you extend your program)
  • Avoid from Tkinter import *. This might lead to unexpected naming clashes.

Complete code

##!/usr/bin/env python

import Tkinter as Tk


class App(object):
    def __init__(self):
        self.root = Tk.Tk()
        self.root.wm_title("Question 7")
        self.label = Tk.Label(self.root, text="Enter your weight in pounds.")
        self.label.pack()

        self.weight_in_kg = Tk.StringVar()
        Tk.Entry(self.root, textvariable=self.weight_in_kg).pack()

        self.buttontext = Tk.StringVar()
        self.buttontext.set("Calculate")
        Tk.Button(self.root,
                  textvariable=self.buttontext,
                  command=self.clicked1).pack()

        self.label = Tk.Label(self.root, text="")
        self.label.pack()

        self.root.mainloop()

    def clicked1(self):
        weight_in_kg = self.weight_in_kg.get()
        self.label.configure(text=weight_in_kg)

    def button_click(self, e):
        pass

App()
Sign up to request clarification or add additional context in comments.

Comments

5

Is this the kinda thing you are looking for?

from Tkinter import *
import tkMessageBox

class App(object):
    def __init__(self):
        self.root = Tk()
        self.root.wm_title("Question 7")
        self.label = Label (self.root, text= "Enter your weight in pounds.")
        self.label.pack()


        self.entrytext = StringVar()
        Entry(self.root, textvariable=self.entrytext).pack()

        self.buttontext = StringVar()
        self.buttontext.set("Calculate")
        Button(self.root, textvariable=self.buttontext, command=self.clicked1).pack()

        self.label = Label (self.root, text="")
        self.label.pack()

        self.root.mainloop()


    def clicked1(self):
        input = self.entrytext.get()
        result = int(input)*2
        self.label.configure(text=result)

    def button_click(self, e):
        pass

App()

I think this is what your'e looking for, although not just times by 2. You would probably also want to put in an exception for if the value is not a int.

Comments

4

As you have associated a StringVar with your Entry widget, you can easily access/manipulate the widget's text with StringVar's get and set methods.

See here for more information.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.