如何从Tkinter获取输入

use*_*056 2 python tkinter

我正在使用Tkinter创建一个程序,用户在Pound中输入它们的重量,然后以千克输出它们的重量.

我在Entry从用户那里获取内容时遇到问题.我正在计算英镑到公斤clicked1.

有人可以告诉我如何获得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()
Run Code Online (Sandbox Code Playgroud)

jba*_*win 6

这是你要找的那种东西吗?

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()
Run Code Online (Sandbox Code Playgroud)

我认为这是你正在寻找的,虽然不只是2的时间.如果值不是int,你可能还想要一个例外.


Mar*_*oma 5

你在寻找什么 [widget].get()

文本小部件

在您使用文本小的情况下,你必须使用[widget].get(1.0, END)其中1.0的意思是"第一线,第0个字符"

代码审查

我注意到代码中的其他一些内容可以改进:

  • PEP8符合性; 请参阅pep8online.com
  • 如果添加一个Shebang,Linux用户将能够直接执行它./script.py.
  • 变量命名:
    • input 是一个内置函数,你应该避免覆盖它
    • 使用有意义的变量名称(如果扩展程序,entrytext可能会有问题)
  • 避免from Tkinter import *.这可能会导致意外的命名冲突.

完整的代码

##!/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()
Run Code Online (Sandbox Code Playgroud)