-3

Tkinter Python 3.x

Here is my code

def enter():
    if w.get() == 777:
        messagebox.showinfo("Spinbox","The Spinbox has max number")
    else:
        messagebox.showinfo("Spinbox","Your number " + w.get() + " is not max.")
root = Tk() 
w = Button(root, text='Check if max', command=enter) 
w.pack()
mainloop()

Output:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:\Users\brand\Desktop\Gobotz\gobotz.systemid.py", line 28, in enter
    if w.get() == 777:
AttributeError: 'Button' object has no attribute 'get'

So please help me on this code.

khelwood
  • 55,782
  • 14
  • 81
  • 108
C.Flance
  • 17
  • 1
  • 5
  • Button is a clickable object. How do you expect for it to have a value? – Swetank Poddar Jan 03 '20 at 14:59
  • 1
    Your error message mentions a spinbox, but your code doesn't have a spinbox. Are you wanting to get a value from a button like in your code, or are you wanting to get the value of a spinbox? – Bryan Oakley Jan 03 '20 at 15:19

1 Answers1

1

As Swetank pointed out, the get method is intended for a variety of widgets however the Button class is not one of them. I've included a short working example using the Spinbox widget.

from tkinter import Tk, Button, messagebox, Spinbox

def enter():
    if spin.get() == '777':
        messagebox.showinfo("Spinbox","The Spinbox has max number")
    else:
        messagebox.showinfo("Spinbox","Your number " + spin.get() + " is not max.")
root = Tk()
spin = Spinbox(root, from_=0, to=777)
spin.pack()
w = Button(root, text='Check if max', command=enter) 
w.pack()
root.mainloop()
Axe319
  • 4,255
  • 3
  • 15
  • 31