简体   繁体   English

如何从Tkinter获取输入

[英]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()

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" 如果你使用文本小部件,你必须使用[widget].get(1.0, END)其中1.0表示“第一行,第0个字符”

Code review 代码审查

I've noticed a few other things in your code that could get improved: 我注意到代码中的其他一些内容可以改进:

  • PEP8 conformity; PEP8符合性; see pep8online.com 请参阅pep8online.com
  • If you add a Shebang , Linux users will be able to execute it directly with ./script.py . 如果添加一个Shebang ,Linux用户将能够直接使用./script.py执行它。
  • Variable naming: 变量命名:
    • input is a built-in function and you should avoid overwriting it input是一个内置函数,你应该避免覆盖它
    • Use meaningful variable names (entrytext might be problematic in case you extend your program) 使用有意义的变量名称(如果扩展程序,entrytext可能会有问题)
  • Avoid from Tkinter import * . 避免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()

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. 由于您已将StringVarEntry小部件相关联,因此您可以使用StringVar的get和set方法轻松访问/操作小部件的文本。

See here for more information. 有关更多信息,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM