Reputation: 623
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()
Upvotes: 2
Views: 23024
Reputation: 924
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.
Upvotes: 5
Reputation: 56
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.
Upvotes: 4
Reputation: 136177
What you are looking for is [widget].get()
In case you use the Text widget, you have to use [widget].get(1.0, END)
where 1.0
means "first line, 0th character"
I've noticed a few other things in your code that could get improved:
./script.py
.input
is a built-in function and you should avoid overwriting itfrom Tkinter import *
. This might lead to unexpected naming clashes.##!/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()
Upvotes: 5