2

Tkinter を使用して、ユーザーが体重をポンド単位で入力し、体重をキロ単位で出力するプログラムを作成しています。

ユーザーからのコンテンツを取得する際に問題が発生してEntryいます。私はポンドをキロで計算していclicked1ます.

誰かがそこにエントリ入力を取得する方法を教えてもらえますか?

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

3 に答える 3

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

これはあなたが探しているものだと思いますが、2倍だけではありません。値がintでない場合は、おそらく例外を入れたいと思うでしょう。

于 2012-10-29T10:54:33.590 に答える
5

あなたが探しているのは[widget].get()

テキストウィジェット

Text ウィジェットを使用する場合は、「最初の行、0 番目の文字」を意味する[widget].get(1.0, END)whereを使用する必要があります。1.0

コードレビュー

あなたのコードには、改善できる可能性のあるいくつかの点があることに気付きました。

  • PEP8準拠; pep8online.comを参照
  • Shebangを追加すると、Linux ユーザーは で直接実行できるようになります./script.py
  • 変数の命名:
    • inputは組み込み関数であり、上書きを避ける必要があります
    • 意味のある変数名を使用してください (プログラムを拡張する場合、エントリテキストが問題になる可能性があります)
  • 避けてください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()
于 2014-08-10T19:46:12.313 に答える
4

をウィジェットに関連付けたStringVarのでEntry、StringVar の get および set メソッドを使用して、ウィジェットのテキストに簡単にアクセス/操作できます。

詳しくはこちらをご覧ください。

于 2012-10-29T00:18:15.597 に答える