Problems with python 3

Asked

Viewed 39 times

-1

Good night!

I’m having a small problem with python 3.x on my linux Mint 19.03


Code:

#!/usr/bin/env python3
#-*-coding: utf-8-*-
from tkinter import *
win = Tk()
def msg():
  m = val.get()
  Label(win, text=m).pack()      
win.geometry("250x250+250+250")
win.title("aula excript")
val = Entry(win).pack()      
Button(win, text="ok", command=msg).pack()      
win.mainloop()

The above code is generating the following exception:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.6/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "./tt.py", line 6, in msg
    m = val.get()
AttributeError: 'NoneType' object has no attribute 'get'

Thanks in advance =D

1 answer

0


The error occurs as you are inserting the return of the method pack in the variable val, whereas pack does not have a return (None):

val = Entry(win).pack()

Thus generating the error:

Attributeerror: 'Nonetype' Object has no attribute 'get'


When in fact, val should contain only the return of Entry, and with that value then call the method pack:

val = Entry(win)
val.pack()

With this, the method get will return the value correctly, failing the error.


Your code then would look like this:

#!/usr/bin/env python3
#-*-coding: utf-8-*-

from tkinter import *

win = Tk()

def msg():
  m = val.get()
  Label(win, text=m).pack()

win.geometry("250x250+250+250")
win.title("aula excript")
val = Entry(win)
val.pack()
Button(win, text="ok", command=msg).pack()      
win.mainloop()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.