【问题标题】:Can't get contents of an Entry widget using input()无法使用 input() 获取 Entry 小部件的内容
【发布时间】:2015-04-28 16:01:13
【问题描述】:

我正在尝试使用 python 3.4.2 在 Windows 8.1 中编写一个简单的 python gui。
我尝试制作一个程序来计算浓度(摩尔浓度 = 摩尔/升),但在我创建的 GUI 中,文本小部件中没有出现答案,但命令外壳中出现数字。
计算似乎也不起作用,因为当我将条目留空时,计算了一些东西(这应该是不可能的,即使空条目的计算结果为 0,它也不应该除以 0)并且它给了我这些数字.56494480.56494448.

我认为问题出在这部分

def mol(self):
    moli = float(input(self.grammi)) / float(input(self.peso_molecolare))
    self.text.delete(0.0, END)
    self.text(0.0, moli)


def mola(self):
    conc = float(float(input(self.grammi)/ float(input(self.peso_molecolare))) / float(input(self.litri))

    self.text.delete(0.0, END)
    self.text.insert(0.0, conc)

如果你想要完整的代码,那就是

from tkinter import *


class Application(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.instuction = Label(self, text="inserisci i seguenti dati")
        self.instuction.grid(row=0, column=0, columnspan=2, sticky=W)
        self.grammi = Entry(self)
        self.grammi.label = Label(self, text="grammi")
        self.grammi.grid(row=1, column=1, sticky=W)
        self.grammi.label.grid(row=1, column=0, sticky=W)
        self.peso_molecolare = Entry(self)
        self.peso_molecolare.label = Label(self, text="peso molecolare")
        self.peso_molecolare.grid(row=2, column=1, sticky=W)
        self.peso_molecolare.label.grid(row=2, column=0, sticky=W)
        self.litri = Entry(self, text="litri")
        self.litri.label = Label(self, text="litri")
        self.litri.grid(row=3, column=1, sticky=W)
        self.litri.label.grid(row=3, column=0, sticky=W)

        self.moli_button = Button(self, text="calcolo moli", command=self.mol)
        self.moli_button.grid(row=2, column=2, sticky=W)
        self.conc_button = Button(self, text="concentrazione", command=self.mola)
        self.conc_button.grid(row=3, column=2, sticky=W)
        self.exit_button = Button(self, text="Exit", command=self.close_window)
        self.exit_button.grid(row=4, column=2, sticky=W)

        self.text = Text(self, width=35, height=5, wrap=NONE)
        self.text.grid(row=4, column=0, columnspan=2, sticky=W)

    def mol(self):
        moli = float(input(self.grammi)) / float(input(self.peso_molecolare))
        self.text.delete('1.0', END)
        self.text.insert('1.0', moli)


    def mola(self):
        conc = float(float(input(self.grammi)) / float(input(self.peso_molecolare))) / float(input(self.litri))
        self.text.delete('1.0', END)
        self.text.insert('1.0', conc)

    def close_window(self):
        root.destroy()


root = Tk()
root.title("chimica")
root.geometry("400x200")
app = Application(root)
root.mainloop()

【问题讨论】:

  • 你在使用 wxPython 吗?
  • 请提供更多代码和更多上下文。一个简短的、可运行的示例以及对您期望发生的事情的解释将使您的问题有可能得到回答。此外,看起来您正在使用 Tkinter。如果这是真的,您应该相应地标记您的问题。
  • 我已投票结束,因为您没有改进您的问题。
  • Python 3.4.2 for win 8.1

标签: python tkinter


【解决方案1】:

使用 Tkinter,要获取插入到 Entry 小部件中的值,您不应该使用 input,但您必须使用 get 方法,例如:

moli = float(self.grammi.get()) / float(self.peso_molecolare.get())

conc 也是如此:

conc = float(self.grammi.get()) / float(self.peso_molecolare.get()) / float(self.litri.get())

您遇到的问题是input 在询问括号之间的问题后,会在命令外壳中提示用户输入。但是,您在此处放置了对 Entry 小部件的引用。所以打印的内容(.56494480.56494448)是对这些小部件的内部引用,而不是任何计算的结果。

【讨论】:

    【解决方案2】:

    我必须假设您正在使用 Tkinter。可能是行号从 1 开始,而您在第 0 行插入。此外,索引是字符串,而不是浮点数。

    尝试将您的代码更改为:

    def mol(self):
        moli = float(input(self.grammi)) / float(input(self.peso_molecolare))
        self.text.delete('1.0', END)
        self.text.insert('1.0', moli)
    
    def mola(self):
        conc = float(float(input(self.grammi)) / float(input(self.peso_molecolare))) / float(input(self.litri))
        self.text.delete('1.0', END)
        self.text.insert('1.0', conc)
    

    或者你可以只使用self.text.insert(INSERT, conc),它将插入到当前插入点。

    【讨论】:

    • 似乎 Tkinter 对索引比文档更宽容,但至少现在您的代码与文档一致。我认为fhdrsdg's 的答案是正确的——你需要使用.get(),而不是input()
    • 虽然不能解决问题,但您的观察非常真实。文本索引应该是字符串,而不是浮点数。这导致了a lot of confusion,因为它们在某些情况下工作,but fail in a lot more
    猜你喜欢
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 2015-05-10
    • 2022-12-08
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    相关资源
    最近更新 更多