我正在學習基本的Python過程。我目前正試圖創建一個簡單的計算器程序,只有加法和減法。我有一個問題,但。我不知道如何在按下按鈕時將文本添加到我的Python標籤。現在,按下'1'按鈕後,我的程序將顯示標籤更改爲文本「1」。但是,我希望我的程序添加文本,而不是設置它。我將如何修改/添加文本到tkinter.Label?
例如,如果我按下'按鈕1'5次,它當前將重置標籤文本5次,並且將導致單個1.我希望它在按下時將標籤添加到標籤,而不是替換。後
當前結果按下按鈕5倍:按壓按鈕5次後1
請求的結果:
這裏是我的編程電流的代碼。如果有什麼不清楚的地方,就問一下。謝謝。
from tkinter import *
window = Tk()
# Creating main label
display = Label(window, text="")
display.grid(row=0, columnspan=3)
def add_one():
display.config(text='1')
# Creating all number buttons
one = Button(window, text="1", height=10, width=10, command=add_one)
two = Button(window, text="2", height=10, width=10)
three = Button(window, text="3", height=10, width=10)
four = Button(window, text="4", height=10, width=10)
five = Button(window, text="5", height=10, width=10)
six = Button(window, text="6", height=10, width=10)
seven = Button(window, text="7", height=10, width=10)
eight = Button(window, text="8", height=10, width=10)
nine = Button(window, text="9", height=10, width=10)
zero = Button(window, text="0", height=10, width=10)
# Placing all number buttons
one.grid(row=1, column=0)
two.grid(row=1, column=1)
three.grid(row=1, column=2)
four.grid(row=2, column=0)
five.grid(row=2, column=1)
six.grid(row=2, column=2)
seven.grid(row=3, column=0)
eight.grid(row=3, column=1)
nine.grid(row=3, column=2)
# Creating all other buttons
add = Button(window, text="+", height=10, width=10)
subtract = Button(window, text="-", height=10, width=10)
equal = Button(window, text="=", height=10, width=10)
# Placing all other buttons
add.grid(row=4, column=0)
subtract.grid(row=4, column=1)
equal.grid(row=4, column=2)
window.mainloop()
只要刪除以前所有的標籤文本,然後添加新的文本。 –
使用變量來控制標籤文本的值。 –