Tkinter计算器与更改文本按钮

时间:2017-02-07 16:53:45

标签: python tkinter

我是Python的新手,我正在尝试使用德州仪器计算器作为我的功能和功能灵感。目前,我正在尝试制作一个移位按钮,将按钮的所有文本更改为另一组文本。为了简化测试,我只使用一个按钮,其他按钮被注释掉了。我运行我的程序并收到以下错误消息:Traceback(最近一次调用最后一次):   文件“/Users/ryanflynn/shCalctest.py”,第18行,in     txt = root.StringVar()   文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/init.py”,第2095行, getattr     return 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()

任何帮助都将不胜感激!!

1 个答案:

答案 0 :(得分:1)

  1. 您从tkinter导入了所有内容 - 无需tk.StringVar
  2. variabletext = txt[0] - 它的textvariable并且此处不需要编制索引。
  3. ** a = ([1,2,3,4,5,6,7,8,9,0],[A,B,C,D,E,F,G,H,I,J]) - 字母作为变量预先设定,未定义。您可以使用['A','B','C','D','E','F','G','H','I','J']替换它们。
  4. 更改这两行:

    txt = StringVar()
    one = Button(root, textvariable = txt, command = lambda : print(txt))