I'm working on getting a python/tkinter label widget to update its contents. Per an earlier thread today, I followed instructions on how to put together the widgets. At runtime, however, the label widget does NOT change contents, when I click button Calculeaza. As far as I can tell, function Calculeaza() is wrong.
def Calculeaza(self):
cgrade =celsiusEntry.get()
if cgrade == ' ':
fahrenheitEntry.configure(text = ' ')
else:
cgrade=float(cgrade)
fgrade=(cgrade-32)/1.8
fahrenheitEntry.configure(text=str(fgrade))# is not function
This is the code:
import sys
from Tkinter import *
class C2F(Frame):
#celsiusEntry = Entry
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("Convertor Celsius/Fahrenheit")
self.pack(fill=BOTH, expand=1)
# Meniul superior cu File>Exit si Help>About
menuBar= Menu(self.parent)
self.parent.config(menu=menuBar)
fileMenu= Menu(menuBar)
fileMenu.add_command(label="Exit", command = self.onExit)
menuBar.add_cascade(label="File", menu=fileMenu)
# Adaugare butoane http://effbot.org/tkinterbook/grid.htm
"""
Label(self.parent, text="First").grid(row=0, column =0)
Label(self.parent, text="First").grid(row=1, column = 0)
"""
labelframe = LabelFrame(self.parent, text="Celsius/Fahrenheit")
labelframe.pack(fill="both", expand="yes")
left = Label(labelframe, text="Celsius")
left.grid(row=0, column=0)
Label(labelframe, text="Fahrenheit").grid(row=1, column =0)
global celsiusEntry
celsiusEntry=Entry(labelframe, bd=5)
celsiusEntry.grid(row=0, column=1)
global fahrenheitEntry
fahrenheitEntry=Entry(labelframe, bd=5, text="salut")
fahrenheitEntry.grid(row=1, column=1)
calcButon = Button(labelframe, text="Calculeaza", command=self.Calculeaza)
calcButon.grid(row=1, column=2)
def onExit(self):
self.parent.quit()
def Calculeaza(self):
cgrade =celsiusEntry.get()
if cgrade == ' ':
fahrenheitEntry.configure(text = ' ')
else:
cgrade=float(cgrade)
fgrade=(cgrade-32)/1.8
fahrenheitEntry.config(text=str(fgrade))# is not function
def main():
root= Tk()
root.geometry("350x350+300+300")
app= C2F(root)
#Label(root, text="First").grid(row=0, column =0)
root.mainloop()
if __name__ == "__main__":
main()