+1 vote
ago by
Hi, I don't know much about python yet, so please help me determine where the error is.
I wanted to be able to enter a radius value into the Entry widget, which would then become a numeric variable and, when multiplied by Pi, display the circle value in the console. In the code I use tkinter. But when I run the program it outputs :
r = int(radius.get())
NameError: name 'radius' is not defined

Here is the code (def kr_click():):
def radius_click():
r = int(radius.get())
okr = r*3.14
print(okr)

def kr_click():
frame_kr = Frame(root, bg='#060922')
frame_kr.place(relx=0.68, rely=0, relwidth=0.32, relheight=1)
radius = Entry(frame_kr, bg='white', font=40)
radius.place(relx=0.1, rely=0.30)
kradius = Button(frame_kr, text='Help', bg='yellow', command=radius_click)
kradius.place(relx=0.5, rely=0.45)
(I've never asked questions before, post if there's not enough data)

2 Answers

0 votes
ago by
 
Best answer
The radius variable exists in the namespace of the kr_click function, but not in radius_click. It must be passed to:
def radius_click(radius):
...

kradius = Button(frame_kr, text='Help', bg='yellow', command=lambda: radius_click(radius))
0 votes
ago by
1.It is necessary to specify all the code and not a part of it where the error occurred! or better both the code and the part where there is an error.
2.Watch the youtube video about scopes in python.
3.The error occurred because the radius variable is local and is used only in this function.
I corrected the error by specifying that the radius variable is global
Here's the code itself
from tkinter import*

root = Tk()
root.title("какое-то ")
root.geometry("450x450")

def radius_click():
r = int(radius.get())
okr = r*3.14
print(okr)

def kr_click():
global radius
frame_kr = Frame(root, bg='#060922')
frame_kr.place(relx=0.68, rely=0, relwidth=0.32, relheight=1)
radius = Entry(frame_kr, bg='white', font=40)
radius.place(relx=0.1, rely=0.30)
kradius = Button(frame_kr, text='Help', bg='yellow', command=radius_click)
kradius.place(relx=0.5, rely=0.45)

kr_click()
5.If it helped, mark the comment with a decision. 6.google to help
...