【发布时间】:2017-02-08 00:57:33
【问题描述】:
我是 Python 新手,我正在尝试使用德州仪器 (Texas Instruments) 的计算器制作一个计算器,作为我对函数和功能的灵感。目前,我正在尝试制作一个转换按钮,将按钮的所有文本更改为一组不同的文本。为了简化测试,我只使用一个按钮,其他按钮被注释掉。我运行我的程序并收到以下错误消息: Traceback(最近一次调用最后一次): 文件“/Users/ryanflynn/shCalctest.py”,第 18 行,在 txt = root.StringVar() getattr 中的文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/init.py”,第 2095 行 返回 getattr(self.tk, attr) AttributeError: '_tkinter.tkapp' 对象没有属性 'StringVar'
这是我的代码:
from tkinter import *
import time
root = Tk()
root.title('Calculator')
mode = 0
display = Entry(root)
display.grid(row = 1, columnspan = 6)
def txtupdate(mode):
a = ([1,2,3,4,5,6,7,8,9,0],[A,B,C,D,E,F,G,H,I,J])
txt.set(a[mode])
return
txt = tk.StringVar()
one = Button(root, variabletext = txt[0], command = lambda : print(txt))
txt.set([1,2,3,4,5,6,7,8,9,0])
one.grid(row = 2, column = 0)
'''two = Button(root, text = '2', command = lambda : print('2'))
two.grid(row = 2, column = 1)
three = Button(root, text = '3', command = lambda : print('3'))
three.grid(row = 2, column = 2)
four = Button(root, text = '4', command = lambda : print('4'))
four.grid(row = 3, column = 0)
five = Button(root, text = '5', command = lambda : print('5'))
five.grid(row = 3, column = 1)
six = Button(root, text = '6', command = lambda : print('6'))
six.grid(row = 3, column = 2)
seven = Button(root, text = '7', command = lambda : print('7'))
seven.grid(row = 4, column = 0)
eight = Button(root, text = '8', command = lambda : print('8'))
eight.grid(row = 4, column = 1)
nine = Button(root, text = '9', command = lambda : print('9'))
nine.grid(row = 4, column = 2)
zero = Button(root, text = '0', command = lambda : print('0'))
zero.grid(row = 5, column = 1)'''
shift = Button(root, text = 'sft', command = txtupdate(1))
shift.grid(row = 2, column = 1)
root.mainloop()
任何帮助将不胜感激!
【问题讨论】:
-
你能提供一份错误的副本吗?通常,如果系统文件(在您的情况下是 tkinter 的
__init__.py文件)中出现错误,通常是因为您的代码中的参数有问题。 -
请选择一个答案或对无效的答案发表评论。