Python: tkinter中的get()方法不起作用

huangapple go评论96阅读模式
英文:

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()

huangapple
  • 本文由 发表于 2020年1月3日 22:57:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定