【问题标题】:how to call a function when click on button in pyfltk单击pyfltk中的按钮时如何调用函数
【发布时间】:2021-05-10 13:15:57
【问题描述】:

我想在按下Fl_Button 时调用一个函数。怎么样?

这是示例代码:

from fltk import *
win = Fl_Window(150, 200, 'Test')

def test():
     print("Function Called") # I want this function to be called when press the btn

btn = Fl_Button(40, 120, 60, 30, 'Resault'); res_btn.box(FL_PLASTIC_UP_BOX)

win.show(sys.argv); Fl_run()

谢谢!

【问题讨论】:

  • 如果你知道请帮忙。

标签: python user-interface fltk


【解决方案1】:

使用 button.callback 方法。

请参阅回调部分中的https://pyfltk.sourceforge.io/docs/CH3_Common.html

【讨论】:

  • 很好,但如果我想要快捷方式?
  • 快捷方式在回调之后的下一部分。您没有在原始问题中询问快捷方式。我猜你还没有读过它——它只有 5 行不包括示例,否则你会看到快捷方式。
【解决方案2】:

您是否注意到 pyFltk 附带的示例?有大量示例,其中许多解决了您当前正在研究的非常具体的问题。

例如,这是一个名为 button.py 的示例,它突出显示了您可以使用回调执行的更多操作:

from fltk import *
import sys

window = None

class MyButton(Fl_Button):
    data = "My Secret Data"
    def __init__(self, x, y, w, h, l):
        Fl_Button.__init__(self, x, y, w, h, l)
    
def beepcb(ptr, widget):
    print("beepcb: ")
    print("  Widget member: ",ptr.data)

def exitcb(ptr, widget):
    sys.exit(0)

window = Fl_Window(100,100,320, 65,"Button")
b1 = MyButton(20,20,80,25, "Beep")

b1.callback(beepcb, window)
b2 = Fl_Button(120,20, 80, 25, "&no op")
b3 = Fl_Button(220,20, 80, 25, "Exit");
b3.callback(exitcb,window)
window.end()

window.show()
Fl.run()

祝你好运

安德烈亚斯

【讨论】:

    猜你喜欢
    • 2020-12-23
    • 1970-01-01
    • 2020-07-29
    • 2021-05-05
    • 1970-01-01
    • 2013-02-26
    • 2014-01-11
    • 2014-08-24
    • 1970-01-01
    相关资源
    最近更新 更多