I have a Label which i want to use as the display for my calculator - when a button is pressed I want the display to be updated. I am trying to set the displayText so I should be able to type 01 or 10.
from tkinter import *
gui = Tk()
buttonValues = []
displayText = StringVar(gui)
def press(buttonValue):
buttonValues.append(buttonValue)
display = Label(gui, text=displayText.set(''.join(str(i) for i in buttonValues)))
display.grid(row=0)
display.update()
"print(''.join(str(i) for i in buttonValues))"
if __name__ == "__main__":
button0 = Button(gui, text=' 0 ', fg='black', bg='red',
command=lambda: press(0), height=1, width=7)
button0.grid(row=2, column=0)
button1 = Button(gui, text=' 1 ', fg='black', bg='red',
command=lambda: press(1), height=1, width=7)
button1.grid(row=2, column=1)
gui.mainloop()
You can get it to happen by setting the Label
textvariable
option to the displayText
string variable and then calling its set()
method whenever you want it to change. It's not necessary to repeatedly create a display
widget nor do anything else — it will happen automatically.
from tkinter import *
gui = Tk()
buttonValues = []
displayText = StringVar(value='')
def press(buttonValue):
buttonValues.append(buttonValue)
displayText.set(''.join(map(str, buttonValues)))
if __name__ == "__main__":
display = Label(gui, textvariable=displayText)
display.grid(row=0)
button0 = Button(gui, text=' 0 ', fg='black', bg='red', command=lambda: press(0))
button0.grid(row=2, column=0)
button1 = Button(gui, text=' 1 ', fg='black', bg='red', command=lambda: press(1))
button1.grid(row=2, column=1)
gui.mainloop()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.