0

I've defined an Entry in def then use it in simple math function. When I type in number in Entry it says name (of Entry) is not defined.

I'm very new at programing and this is an school project. In this project I'm trying to make value calculator.

marka1=float(marka.get())*1*3.79
NameError: name 'marka' is not defined

Code:

from  tkinter import*

prozor1=Tk()
prozor1.title('Python')
prozor1.geometry('500x500')
prozor1.config(bg= 'yellow')
natpis1=Label(prozor1, text='Konverter valuta')
natpis1.place(x=200, y=50)
natpis1.config(font=36)
natpis1.config(bg='silver')
natpis1.config(fg='black')
suma1=Entry(prozor1, width=19)
suma1.place(x=200,y=100)
mb=Menubutton(prozor1, text='Valuta')
mb.place(x=27, y=100)
mb.menu=Menu(mb)
mb.config(bg='grey')
mb['menu']=mb.menu

def km1():
    marka1=float(marka.get())*1*3.79
    rezu3=Label(prozor1, text='KN='+str(marka1))
    rezu3.place(x=200, y=170)
    rezu3.config(bg='silver')
def KM1():
    frk=Button(prozor1, text='Pretvori',width=15, command=km1)
    frk.place(x=200,y=130)
    frk.config(bg='silver')
    natpis1=Label(prozor1, text='Unesi sumu u KM')
    natpis1.place(x=90, y=100)
    natpis1.config(bg='silver')
    marka=Entry(prozor1, width=19)
    marka.place(x=200,y=100)

mb.menu.add_command(label='KM-KN', command=KM1)

This is an important part of the project.

martineau
  • 119,623
  • 25
  • 170
  • 301
doVla_
  • 17
  • 1
    `marka` only exists within the scope of KM1, it doesnt exist in the scope of km1 – Chris Doyle Feb 26 '20 at 22:07
  • `import *` is generally bad practice. You should use more whitespace in your program, it's very difficult to parse, it feels like a brick wall. – AMC Feb 27 '20 at 02:00

2 Answers2

1

In your case marka needs to be passed into the function. In order to do this you will need to create the variable earlier in your code. Here is what works for me:

def km1(marka):
    marka1=float(marka.get())*1*3.79
    rezu3=Label(prozor1, text='KN='+str(marka1))
    rezu3.place(x=200, y=170)
    rezu3.config(bg='silver')
def KM1():
    marka=Entry(prozor1, width=19)
    marka.place(x=200,y=100)
    frk=Button(prozor1, text='Pretvori',width=15, command=lambda: km1(marka))
    frk.place(x=200,y=130)
    frk.config(bg='silver')
    natpis1=Label(prozor1, text='Unesi sumu u KM')
    natpis1.place(x=90, y=100)
    natpis1.config(bg='silver')

(Also, this does present another issue with form validations, this might help)

Parakiwi
  • 591
  • 3
  • 19
  • 2
    Passing the function as an argument is a good suggestion, but you're doing it wrong. The `command=km1(marka)` is going to call `km1()` _when the `Button` is being created_ and set its callback function to `None` because that's what `kmb1()` returns. A correct way would be `command=lambda: km1(marka)`. – martineau Feb 26 '20 at 22:39
  • 1
    Spot on. Thanks for the correction! Have edited the response. – Parakiwi Feb 26 '20 at 22:44
0

In python there are something called local and global variables. You define marka inside a function, so the rest of your code can't see it. At the beginning of your def KM1 you need to insert a line like this:

def KM1:
    global marka
    […]

So that the variable becomes global, and your whole code can see it

martineau
  • 119,623
  • 25
  • 170
  • 301
User 12692182
  • 927
  • 5
  • 16
  • 1
    While this would work, it's best to avoid global variables as much as possible — and there are ways to solve the problem and avoid them. – martineau Feb 26 '20 at 22:42