英文:
Python: get() is not working on tkinter python
问题
这是我的代码:
def enter():
if w.get() == 777:
messagebox.showinfo("Spinbox", "Spinbox的最大值")
else:
messagebox.showinfo("Spinbox", "你的数字 " + w.get() + " 不是最大值。")
root = Tk()
w = Button(root, text='检查是否为最大值', command=enter)
w.pack()
mainloop()
输出:
Tkinter回调中的异常
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'对象没有'get'属性
请帮助我解决这段代码的问题。
英文:
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.
答案1
得分: 1
正如Swetank所指出的,get
方法适用于各种小部件,但Button
类不是其中之一。
我已经包含了一个使用Spinbox
小部件的简短示例。
from tkinter import Tk, Button, messagebox, Spinbox
def enter():
if spin.get() == '777':
messagebox.showinfo("Spinbox", "Spinbox的最大值为777")
else:
messagebox.showinfo("Spinbox", "您输入的数字 " + spin.get() + " 不是最大值。")
root = Tk()
spin = Spinbox(root, from_=0, to=777)
spin.pack()
w = Button(root, text='检查是否为最大值', command=enter)
w.pack()
root.mainloop()
英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论