2016-08-05 115 views
3

我正在學習基本的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() 
+0

只要刪除以前所有的標籤文本,然後添加新的文本。 –

+0

使用變量來控制標籤文本的值。 –

回答

2

你應該爲此使用一個StringVar。您的回調需要獲取StringVar的當前內容,對其進行修改,然後使用修改後的字符串設置StringVar的新值。就像這樣:

import tkinter as tk 

window = tk.Tk() 

# Creating main label 
display_text = tk.StringVar() 
display = tk.Label(window, textvariable=display_text) 
display.grid(row=0, columnspan=3) 

def add_one(): 
    s = display_text.get() 
    s += '1' 
    display_text.set(s) 

one = tk.Button(window, text="1", height=10, width=10, command=add_one) 
one.grid(row=1, column=0) 

window.mainloop() 

BTW,你應該儘量讓你的程序的一個小更緊湊的使用for循環來創建和佈置您的按鈕,按照DRY原則。

此外,使用from tkinter import *並不是一個好主意。它將130多個名稱導入名稱空間,如果您不小心將Tkinter名稱用於您自己的某個變量或函數,則可以輕鬆地創建名稱衝突。

+1

我不會說「_應該使用'StringVar'; _can_會更加準確,您可以使用或不使用'StringVar'。 –

+0

謝謝,完美的工作你有什麼建議我改變我的進口爲此(而不是:從tkinter導入*) – jxshu

+0

好點,@BryanOakley。我只是覺得它更容易使用StringVar和朋友:) –

0

這是你在找什麼:

from tkinter import * 
root = Tk() 

var = StringVar() 

def f1(): 
    var.set(" ") 
    var.set("1") 

def f2(): 
    var.set(" ") 
    var.set("2") 

label = Label(root, textvariable=var) 
label.pack() 

button1 = Button(root, text="One", command=f1) 
button1.pack() 

button2 = Button(root, text="Two", command=f2) 
button2.pack() 

1

您可以定義add_one像下面,首先得到現有的值,然後一個新值追加到它:

def add_one(): 
    current_value = display.cget("text") 
    new_value = current_value + "1" 
    display.config(text=new_value)