Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] how do I print cost variable on GUI?
#1
how do I print cost variable on GUI? it says: cost is not defined. Thank you!

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.label_3 = Label(self, text = Cost)
        self.entry_1 = Entry(self)
        self.entry_2 = Entry(self)
        
        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) 
        
            
    def savings(self):
        return (self.label_1 - self.label_2)
    

root = Tk()
root.title("Cost calculator")
root.geometry("500x200")
app = Application(root)
root.mainloop()
Quote
#2
It looks like cost isn't defined anywhere in your code
*Describe the environment in which it occurs.                                    *Describe the symptoms of your problem clearly.
*Describe the research you did to try and understand the problem.      *Describe the goal, not the step. 
*Use meaningful, specific subject headers                                          *Write in clear, grammatical, correctly-spelled language
*Describe the problem's symptoms, not your guesses                         *Describe your problem's symptoms in chronological order
*Describe the diagnostic steps you took to try and pin down the problem yourself.
*Describe any possibly relevant recent changes in your computer or software configuration. 
*Provide a way to reproduce the problem in a controlled environment.
Quote
#3
my mistake. It actually should be "savings" instead of "cost"...
but still I have the same problem..
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.label_3 = Label(self, text = savings)
        self.entry_1 = Entry(self)
        self.entry_2 = Entry(self)
        
        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)         
            
    def savings(self):
        return (self.label_1 - self.label_2)    

root = Tk()
root.title("Cost calculator")
root.geometry("500x200")
app = Application(root)
root.mainloop()
Quote
#4
self.label_3 = Label(self, text = savings())
Quote
#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()

*Describe the environment in which it occurs.                                    *Describe the symptoms of your problem clearly.
*Describe the research you did to try and understand the problem.      *Describe the goal, not the step. 
*Use meaningful, specific subject headers                                          *Write in clear, grammatical, correctly-spelled language
*Describe the problem's symptoms, not your guesses                         *Describe your problem's symptoms in chronological order
*Describe the diagnostic steps you took to try and pin down the problem yourself.
*Describe any possibly relevant recent changes in your computer or software configuration. 
*Provide a way to reproduce the problem in a controlled environment.
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)