【发布时间】: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