检查是否单击了按钮

时间:2016-12-19 12:17:13

标签: python pygtk

如何检查按钮是否被点击?这段代码可以工作还是必须使用不同的命令/语法?

def div_clicked(self, button14):
    self.entry.set_text(self.entry.get_text() + str("/"))
    if button14 == "clicked":
        self.equal_clicked()
        self.arithmetic = "division"

特别是这一行:

if button14 == "clicked":

我想知道如何更改代码以便调用self.equal_clicked()函数。

2 个答案:

答案 0 :(得分:0)

正如您的问题被标记为pygtk我假设您使用的是GTK 2.在这种情况下,请查看http://www.pygtk.org/pygtk2tutorial/ch-GettingStarted.html#sec-HelloWorld

这将为您提供关于如何处理按钮点击和其他事件的良好起点。我在下面粘贴了一个稍微剥离的版本:

import pygtk
pygtk.require('2.0')
import gtk

class HelloWorld:

    # This is a callback function. The data arguments are ignored
    # in this example. More on callbacks below.
    def hello(self, widget, data=None):
        print "Hello World"

    def destroy(self, widget, data=None):
        gtk.main_quit()

    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("destroy", self.destroy)

        # Creates a new button with the label "Hello World".
        self.button = gtk.Button("Hello World")

        # When the button receives the "clicked" signal, it will call the
        # function hello() passing it None as its argument.  The hello()
        # function is defined above.
        self.button.connect("clicked", self.hello, None)
        self.window.add(self.button)
        self.button.show()
        self.window.show()

    def main(self):
        # All PyGTK applications must have a gtk.main(). Control ends here
        # and waits for an event to occur (like a key press or mouse event).
        gtk.main()

if __name__ == "__main__":
    hello = HelloWorld()
    hello.main()

答案 1 :(得分:0)

扩展您的代码,您也可以执行此表格

function MyArray() { }
MyArray.prototype = [];

var arr = new MyArray();
arr.push(1, 2, 3);
console.log(arr);                    // { 0: 1, 1: 2, 2: 3, length: 3 }
console.log(typeof arr);             // object
console.log(Array.isArray(arr));     // false
console.log(arr instanceof Array);   // true
console.log(arr instanceof MyArray); // true