我如何将按钮的值存储在变量中?(Python Gtk)

2024-04-20 04:43:25 发布

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

我需要将单击的按钮的值存储到变量中。所以我需要把按钮和变量连接起来,我该怎么做呢?以下是我的变量:

    self.first_num = 0
    self.second_num = 0
    self.result = 0
    self.operation = ""

然后我需要在这个函数中使用它们,例如,另外:

^{pr2}$

我希望这是足够的代码来告诉你我的问题,我会很感激你的回答。在

    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)

Tags: 函数代码selftrueresult按钮operationnum
1条回答
网友
1楼 · 发布于 2024-04-20 04:43:25

我经常为工作制作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:像这样绑定所有按钮:

^{pr2}$

(使用这个部分:lambda event: button_clicked(event, "1")代替通常将按钮连接到函数的位置。)


这里的思想,不管使用的是哪个GUI模块,都是绑定单个函数 对计算器的所有按钮,只需为每个按钮传入不同的值。在

相关问题 更多 >