L
L
lnnaannl2021-10-16 01:05:40
Python
lnnaannl, 2021-10-16 01:05:40

How to overcome the "name is not defined" error?

I am creating a program where there will be a window in which the user writes text, and then presses the "Enter" button, and everything that he wrote is sent to me in the cart.

The code:

#telegram

telegram_send.send( messages = ut )


#stiles

root = Tk()

root.title('Kontra')
root.wm_attributes('-alpha', 1.0)
root.geometry('1000x1000')
root.resizable(width=True, height=True)

root[ 'bg' ] = '#ccc'

#to-text

user_text = Entry( root, font = 'Consolas', relief = 'solid',
 bg = 'silver')

#command

def conn():
 
 ut = user_text.get()

 

btn = Button( text = 'Войти', command = conn )


user_text.pack()
btn.pack()

Mistake:

telegram_send.send( messages = ut )
NameError: name 'ut' is not defined

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2021-10-16
@o5a

1. For the interface to work, you need to run a loop (mainloop)

...
btn.pack()

root.mainloop()

And in order to continue working with the telegram later (if you need its cycle), you can stop the work of tk by calling root.destroy ()
2. You need to call your messages = ut function after the ut variable is set. For example, in the same conn function:
def conn():
    ut = user_text.get()
    root.destroy()
    telegram_send.send( messages = ut )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question