Python Forum
[Tkinter] how do I print cost variable on GUI?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] how do I print cost variable on GUI?
#5
savings is referring to a global function not defined, you have a method called savings...thus you need
self.savings

your also going to need an IntVar at least 1 for the savings value...and probably for each Entry as well. You want a button to calculate at the users request, otherwise it calculates the defaults. So calling savings should be done by a button and not the label. 
from tkinter import *
 
class Application(Frame):
 
    def __init__(self, master):
        Frame.__init__(self,master)
        self.grid()
        self.create_widgets()
 
    def create_widgets(self):
        self.label_1 = Label(self, text="Insert Income")
        self.label_2 = Label(self, text="Insert Cost")
        self.savings_var = IntVar()
        self.label_3 = Label(self, textvariable=self.savings_var)
        self.entry_1 = Entry(self)
        self.entry_2 = Entry(self)
        self.btn = Button(self, text='OK', command=self.savings)
         
        self.label_1.grid(row=0, sticky=E)
        self.label_2.grid(row=1, sticky=E)
        self.label_3.grid(row=2, sticky=E)
 
        self.entry_1.grid(row=0, column=1)
        self.entry_2.grid(row=1, column=1)   
        
        self.btn.grid(row=3, column=1)      
             
    def savings(self):
        value = float(self.entry_1.get()) - float(self.entry_2.get())
        self.savings_var.set(value)
 
root = Tk()
root.title("Cost calculator")
root.geometry("500x200")
app = Application(root)
root.mainloop()
Recommended Tutorials:
Reply


Messages In This Thread
how do I print cost variable on GUI? - by Doug - Jan-15-2017, 10:30 PM
RE: how do I print cost variable on GUI? - by Doug - Jan-15-2017, 11:08 PM
RE: how do I print cost variable on GUI? - by metulburr - Jan-16-2017, 04:40 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020