简体   繁体   中英

Can't get contents of an Entry widget using input()

I'm trying to write a simple python gui in windows 8.1 using python 3.4.2.
I try to make a program to calculate a concentration (molarity = moles / liter) but in the GUI I create No answer appears in the Text widget but numbers appear in the command shell.
The calculations also don't seem to work because when I left the entry empty something was calculated (which should be impossible, even if empty Entries would evaluate to 0 it shouldn't be able to divide by 0) and it gives me these numbers .56494480.56494448 .

I think the problem is in this part

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)

If you want the entire code here it is

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

With Tkinter, to get the value inserted in an Entry widget you shouldn't use input but you have to use the get method like:

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

same goes for conc :

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

The problem you have is that input will prompt for user input in the command shell, after asking the question that is between the parentheses. However, you put a reference to an Entry widget there. So what is printed ( .56494480 and .56494448 ) are internal references to these widgets, not results of any calculation.

I must assume that you are using Tkinter. It might be that line numbers start at 1 and you are inserting at line 0. Also, indexes are strings, not floats.

Try altering your code to:

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)

Or you could just use self.text.insert(INSERT, conc) which will insert at the current insertion point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM