无法使用 input() 获取 Entry 小部件的内容
Can't get contents of an Entry widget using input()
我正在尝试使用 python 3.4.2.
在 windows 8.1 中编写一个简单的 python gui
我尝试制作一个程序来计算浓度(摩尔浓度 = 摩尔/升)但是在我创建的 GUI 中文本小部件中没有出现答案但数字出现在命令 shell.
计算似乎也不起作用,因为当我将条目留空时,计算了一些东西(这应该是不可能的,即使空条目的计算结果为 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()
我必须假定您正在使用 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,要获取插入到 Entry 小部件中的值,您不应该使用 input
but you have to use the 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
会在询问括号之间的问题后提示用户在命令 shell 中输入。但是,您在此处放置了对 Entry 小部件的引用。所以打印的内容(.56494480
和 .56494448
)是对这些小部件的内部引用,而不是任何计算的结果。
我正在尝试使用 python 3.4.2.
在 windows 8.1 中编写一个简单的 python gui
我尝试制作一个程序来计算浓度(摩尔浓度 = 摩尔/升)但是在我创建的 GUI 中文本小部件中没有出现答案但数字出现在命令 shell.
计算似乎也不起作用,因为当我将条目留空时,计算了一些东西(这应该是不可能的,即使空条目的计算结果为 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()
我必须假定您正在使用 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,要获取插入到 Entry 小部件中的值,您不应该使用 input
but you have to use the 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
会在询问括号之间的问题后提示用户在命令 shell 中输入。但是,您在此处放置了对 Entry 小部件的引用。所以打印的内容(.56494480
和 .56494448
)是对这些小部件的内部引用,而不是任何计算的结果。