我需要存储单击一个变量的按钮的值。所以我需要将按钮与变量连接,我该怎么办?以下是我的变量:
self.first_num = 0
self.second_num = 0
self.result = 0
self.operation = ""
然后我需要在这个功能中使用它们,例如另外:
def equal_clicked(self, result):
if self.operation == "plus":
self.result = self.first_num + self.second_num
self.entry.set_text(self.result)
def button2_clicked(self, button2):
self.entry.set_text(self.entry.get_text() + str(2))
我希望这是足够的代码向您展示我的问题,我希望得到答案。
button5 = Gtk.Button(label="5")
button5.connect("clicked", self.button_clicked)
vbox.pack_start(button5, True, True, 0)
vbox.pack_end(button5, True, True, 0)
self.add(button5)
答案 0 :(得分:0)
我为工作制作了很多GUI应用程序。主要思想是将单个函数绑定到每个按钮,但是为每个按钮传递一个不同的变量。
以下是Python Gtk的完整示例:
import pygtk
import gtk
class Example:
def __init__(self):
#Setup window
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_border_width(10)
self.boxSizer = gtk.HBox(False, 0)
#Create widgets
self.button1 = gtk.Button("1")
self.button2 = gtk.Button("2")
self.button3 = gtk.Button("3")
self.buttonPlus = gtk.Button("+")
self.buttonEqual = gtk.Button("=")
self.entry = gtk.Entry(max=0)
#Bind Events to a single function
#You would do this for every button.
#The first parameter in the connect() function is the name,
#the second is the function you are binding,
#and the third is the argument we are passing into the function.
self.button1.connect("clicked", self.button_clicked, 1)
self.button2.connect("clicked", self.button_clicked, 2)
self.button3.connect("clicked", self.button_clicked, 3)
self.buttonPlus.connect("clicked", self.button_clicked, "+")
self.buttonEqual.connect("clicked", self.button_clicked, "=")
#Build Window
self.window.add(self.boxSizer)
self.boxSizer.pack_start(self.button1, True, True, 0)
self.boxSizer.pack_start(self.button2, True, True, 0)
self.boxSizer.pack_start(self.button3, True, True, 0)
self.boxSizer.pack_start(self.buttonPlus, True, True, 0)
self.boxSizer.pack_start(self.buttonEqual, True, True, 0)
self.boxSizer.pack_start(self.entry, True, True, 0)
#Show objects
self.button1.show()
self.button2.show()
self.button3.show()
self.buttonPlus.show()
self.buttonEqual.show()
self.entry.show()
self.boxSizer.show()
self.window.show()
def button_clicked(self, widget, value):
"""Your first function and second function could be combined like this.
What this would do is have a single function that is bound to each button.
The arguments passed into it would be different for each button.
Despite using the 'evil eval' function, I figure this is the route you are going.
This removes the need to (1) have a different function for each button,
and (2) you do not need to store values in variables like this:
self.first_num = 0
self.second_num = 0
self.operation = ""
Your code will be cleaner and easier to modify for future projects.
"""
if (value != None):
if (value != "="):
#Add something to the text
self.entry.set_text(self.entry.get_text() + str(value))
else:
#Evaluate the text
self.result = eval(self.entry.get_text())
self.entry.set_text(str(self.result))
else:
#Clear the text
self.entry.set_text("")
def main(self):
gtk.main()
if __name__ == "__main__":
example = Example()
example.main()
此概念适用于任何 GUI创建包。但是,有些软件包可能比其他软件包要多做一些工作。 例如,wxPython不允许您将参数传递给函数。你可以通过做什么来解决这个限制" Florian Bosch"在Is it possible to pass arguments into event bindings?上说。 (注意:此方法适用于任何不允许您将参数传递给绑定函数的GUI包)
对于,wxPython:绑定所有按钮:
self.Bind(wx.EVT_BUTTON, lambda event: button_clicked(event, "1"), button1)
self.Bind(wx.EVT_BUTTON, lambda event: button_clicked(event, "2"), button2)
self.Bind(wx.EVT_BUTTON, lambda event: button_clicked(event, "3"), button2)
self.Bind(wx.EVT_BUTTON, lambda event: button_clicked(event, "4"), button2)
self.Bind(wx.EVT_BUTTON, lambda event: button_clicked(event, "5"), button2)
self.Bind(wx.EVT_BUTTON, lambda event: button_clicked(event, "6"), button2)
self.Bind(wx.EVT_BUTTON, lambda event: button_clicked(event, "7"), button2)
self.Bind(wx.EVT_BUTTON, lambda event: button_clicked(event, "8"), button2)
self.Bind(wx.EVT_BUTTON, lambda event: button_clicked(event, "9"), button2)
self.Bind(wx.EVT_BUTTON, lambda event: button_clicked(event, "0"), button2)
self.Bind(wx.EVT_BUTTON, lambda event: button_clicked(event, "+"), buttonAdd)
self.Bind(wx.EVT_BUTTON, lambda event: button_clicked(event, "-"), buttonSubtract)
self.Bind(wx.EVT_BUTTON, lambda event: button_clicked(event, "*"), buttonMultiply)
self.Bind(wx.EVT_BUTTON, lambda event: button_clicked(event, "/"), buttonDivide)
self.Bind(wx.EVT_BUTTON, lambda event: button_clicked(event, "="), buttonEqual)
self.Bind(wx.EVT_BUTTON, lambda event: button_clicked(event, None), buttonClear)
(使用此部分:lambda event: button_clicked(event, "1")
代替您通常将按钮连接到某个功能的位置。)
无论使用哪种GUI模块,这里的想法是绑定单个函数 对于您的所有计算器按钮,只需为每个按钮传入不同的值。