[英]How to take input from Tkinter
I'm making a program using Tkinter where the user inputs their weight in Pound and then it outputs their weight in kilo. 我正在使用Tkinter创建一个程序,用户以磅为单位输入重量,然后以千克为单位输出重量。
I'm having problems getting the contents of the Entry
from the user. 我在从用户那里获取Entry
的内容时遇到问题。 I'm calculating the pound to kilo in clicked1
. 我计算英镑到基洛clicked1
。
Can someone show me how I would get the Entry input there? 有人可以告诉我如何获得Entry输入吗?
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()
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. 我认为这是你正在寻找的,虽然不只是2的时间。如果值不是int,你可能还想要一个例外。
What you are looking for is [widget].get()
你要找的是[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" 如果你使用文本小部件,你必须使用[widget].get(1.0, END)
其中1.0
表示“第一行,第0个字符”
I've noticed a few other things in your code that could get improved: 我注意到代码中的其他一些内容可以改进:
./script.py
. 如果添加一个Shebang ,Linux用户将能够直接使用./script.py
执行它。 from Tkinter import *
. 避免from 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.