美文网首页
【PySide2学习笔记】2_一个简单的按键应用(涉及信号和槽)

【PySide2学习笔记】2_一个简单的按键应用(涉及信号和槽)

作者: 4thirteen2one | 来源:发表于2019-04-22 18:40 被阅读0次
#!/usr/bin/python

import sys
from PySide2.QtWidgets import QApplication, QPushButton
from PySide2.QtCore import Slot

# Signals and slots is a Qt feature that lets your graphical widgets 
# communicate with other graphical widgets or your python code.

# logs the message to the console
@Slot()
# The @Slot() is a decorator that identifies a function as a slot.
def say_hello():
    print("Button clicked, Hello!")

# Create the Qt Application
app = QApplication(sys.argv)

# create the clickable button, which is a QPushButton instance
button = QPushButton("Click me")

# The QPushButton has a predefined signal called clicked,
# which is triggered every time the button is clicked.

# connect this signal to the say_hello() function
button.clicked.connect(say_hello)

button.show()

# Run the main Qt loop
app.exec_()

运行结果如下:


按键反馈.png

相关文章

网友评论

      本文标题:【PySide2学习笔记】2_一个简单的按键应用(涉及信号和槽)

      本文链接:https://www.haomeiwen.com/subject/kwmugqtx.html