Reputation: 71
I'm new to Python, and am trying to make a calculator with a Texas Instruments calculator as my inspiration for functions and functionality. Currently, I'm trying to make a shift button that changes all the text of the buttons to a different set of text. For simplicity in testing, I am working with just one button and the others are commented out. I ran my program and got the following error message: Traceback (most recent call last): File "/Users/ryanflynn/shCalctest.py", line 18, in txt = root.StringVar() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/init.py", line 2095, in getattr return getattr(self.tk, attr) AttributeError: '_tkinter.tkapp' object has no attribute 'StringVar'
This is my code:
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()
Any help would be appreciated!!
Upvotes: 0
Views: 360
Reputation: 16184
tk.StringVar
.variabletext = txt[0]
- it's textvariable
and the indexing is unneeded here.a = ([1,2,3,4,5,6,7,8,9,0],[A,B,C,D,E,F,G,H,I,J])
- the letters are precieved as variables, which are not defined. You can replace them with ['A','B','C','D','E','F','G','H','I','J']
.Change these two lines:
txt = StringVar()
one = Button(root, textvariable = txt, command = lambda : print(txt))
Upvotes: 1