如何从 Tkinter 获取输入 [英] How to take input from Tkinter

查看:33
本文介绍了如何从 Tkinter 获取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Tkinter 制作一个程序,其中用户以磅为单位输入体重,然后以公斤为单位输出体重.

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

我在从用户那里获取 Entry 的内容时遇到问题.我在 clicked1 中计算磅到公斤.

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

有人可以告诉我如何在那里获得 Entry 输入吗?

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()

推荐答案

由于您已将 StringVar 与您的 Entry 小部件相关联,您可以轻松访问/操作小部件的带有 StringVar 的 get 和 set 方法的文本.

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.

请参阅此处了解更多信息.

这篇关于如何从 Tkinter 获取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆