【问题标题】:AttributeError: 'str' object has no attribute 'set' (Tkinter)AttributeError:“str”对象没有属性“set”(Tkinter)
【发布时间】:2020-12-09 10:44:06
【问题描述】:

我正在用 tkinter 做计算器,但按钮有问题。当我点击时,它显示如下错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\ADMIN\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1885, in __call__
    return self.func(*args)
  File "C:\Users\ADMIN\Desktop\SSCalc\calc.py", line 19, in <lambda>
    btn1 = tk.Button(root,text='1',height=0,width=5,font=fontBtn,command=lambda:pressBtn(1))
  File "C:\Users\ADMIN\Desktop\SSCalc\calc.py", line 12, in pressBtn
    mathValue.set(mathValue)
AttributeError: 'str' object has no attribute 'set'

这是我的代码:

import tkinter as tk
from tkinter import font as tkFont
from tkinter import StringVar, Entry, Button
from tkinter.ttk import *
import math
root = tk.Tk()
root.title("Simple Calculator")
mathValue = ""
def pressBtn(number):
    global mathValue
    mathValue+=str(number)
    mathValue.set(mathValue)
def mainCalc():
    mathValue = StringVar()
    fontBtn = tkFont.Font(family="Helvetica",size=15,weight='bold')
    inputMath = Label(root,textvariable=mathValue,relief='sunken')
    inputMath.config(text="Enter Your Calculation...", width=50)
    inputMath.grid(columnspan=4,ipadx=100,ipady=15) 
    btn1 = tk.Button(root,text='1',height=0,width=5,font=fontBtn,command=lambda:pressBtn(1))
    btn1.grid(row=1,column=0)
    btn2 = tk.Button(root,text='2',height=0,width=5,font=fontBtn,command=lambda:pressBtn(2))
    btn2.grid(row=1,column=1)
mainCalc()
root.mainloop()

谁能为我找到解决此错误的方法?谢谢!

【问题讨论】:

    标签: python python-3.x tkinter set tkinter-label


    【解决方案1】:

    您的代码中有两个问题:

    • 执行mathValue+=str(number) 会创建一个名为mathValuelocal variable,它是一个字符串。
    • 该行顶部的global mathValue 将其转换为global variable

    因此.get() 不适用于string object

    以下代码有效:

    import tkinter as tk
    from tkinter import font as tkFont
    from tkinter import StringVar, Entry, Button
    from tkinter.ttk import *
    import math
    root = tk.Tk()
    root.title("Simple Calculator")
    mathValue = ""
    
    def pressBtn(number):
        mathValue.set(mathValue.get() + str(number))
    
    def mainCalc():
        global mathValue
        mathValue = StringVar()
        fontBtn = tkFont.Font(family="Helvetica",size=15,weight='bold')
        inputMath = Label(root,textvariable=mathValue,relief='sunken')
        inputMath.config(text="Enter Your Calculation...", width=50)
        inputMath.grid(columnspan=4,ipadx=100,ipady=15) 
        btn1 = tk.Button(root,text='1',height=0,width=5,font=fontBtn,command=lambda:pressBtn(1))
        btn1.grid(row=1,column=0)
        btn2 = tk.Button(root,text='2',height=0,width=5,font=fontBtn,command=lambda:pressBtn(2))
        btn2.grid(row=1,column=1)
    
    mainCalc()
    root.mainloop()
    

    我对此代码做了 2 处更正:

    • 我已将global mathValue 行放入mainCalc。这使得StringVar 变成了global variable
    • 我已将pressBtn 中的两行替换为mathValue.set(mathValue.get() + str(number))。这里,mathValue.get() 获取先前存储在 mathValue 中的值(如果有,则返回 '',如果不存在值),+ str(number) 附加新值。最后mathValue.set 设置新值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-10
      • 2021-10-04
      相关资源
      最近更新 更多