2017-05-22 49 views
-1

即時通訊做簡單的GUI計算器。我有0-9的數字按鈕。我將所有的數字和aritnmetic符號存儲在一個名爲caluclations.string的字符串中。我有一個函數,它執行以下操作:Python和tkinter(按鈕)

newString = "" 
newString += calculations 
newString += (here is the button text(for example if button1 was clicked i will add to newString "1") 
return newString 

我希望當我按下一個按鈕,調用函數與newString返回計算string.Any想法?

+0

「任何想法」對於本網站來說都太廣泛和偏離主題。你做過任何研究嗎?在這個網站上有幾十個與按鈕和計算器有關的問題,並且將按鈕與功能相關聯的方法已有詳細記錄。 –

回答

0

要在一個按鈕上調用一個函數,首先你必須像這樣創建函數;

def Foo(): 
    print("Bar") 

然後當你創建按鈕時,你指定你希望它按下時執行那個函數,就像這樣;

from tkinter import ttk 

my_button= ttk.Button(self, text = "button", command = Foo) 

但是如果你想傳遞參數給你的例子,你可能會這樣做;

def Foo(bar): 
    print(bar) 

my_button_paramaters= ttk.Button(self, text = "button", command = lambda: Foo(paramater)) 
+0

你的最後一個例子有點含糊不清,如果'parameter'不是一個常數,它將不起作用。 –