Python和tkinter(按钮)

2024-04-24 19:33:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做简单的图形用户界面计算器。我有0-9之间的数字按钮。我把所有的数字和算术符号存储在一个名为caluclations的字符串中。我有一个函数,它正在执行以下操作:

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

我想当我按下一个按钮时,调用函数并将newString返回到计算中字符串。有吗思想?你知道吗


Tags: the函数字符串hereis符号button数字
1条回答
网友
1楼 · 发布于 2024-04-24 19:33:41

要调用一个按钮上的函数,首先要创建这样的函数

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))

相关问题 更多 >