PySide——Python图形化界面入门教程(三)

               ——使用内建新号和槽

              ——Using Built-In Signals and Slots

上一个教程中,我们学习了如何创建和建立交互widgets,以及将他们布局的两种不同的方法。今天我们继续讨论Python/Qt应用响应用户触发的事件:信号和槽。

当用户执行一个动作——点击按钮,选择组合框的值,在文本框中打字——这个widget就会发出一个信号。这个信号自己什么都不做,它必须和槽连接起来才行。槽是一个接受信号的执行动作的对象。

连接内建PySide/PyQt信号

Qt widgets有许多的内建信号。例如,当QPushButton被点击的时候,它发出它的clicked信号。clicked信号可以被连接到一个拥有槽功能的函数(只是一个概要,需要更多内容去运行)

 @Slot()
def clicked_slot():
''' This is called when the button is clicked. '''
print('Ouch!') # Create the button
btn = QPushButton('Sample') # Connect its clicked signal to our slot
btn.clicked.connect(clicked_slot)

注意@Slot()装饰(decorator)在clicked_slot()的定义上方,尽管它不是严格需要的,但它提示C++ Qt库clicked_slot应该被调用。(更多decorators的信息参见http://www.pythoncentral.io/python-decorators-overview/)我们之后会了解到@Slot宏更多的信息。现在,只要知道按钮被点击时会发出clicked信号,它会调用它连接的函数,这个函数生动的输出“Ouch!”。

我们接下来看看QPushButton发出它的三个相关信号,pressed,released和clicked。

 import sys
from PySide.QtCore import Slot
from PySide.QtGui import * # ... insert the rest of the imports here
# Imports must precede all others ... # Create a Qt app and a window
app = QApplication(sys.argv) win = QWidget()
win.setWindowTitle('Test Window') # Create a button in the window
btn = QPushButton('Test', win) @Slot()
def on_click():
''' Tell when the button is clicked. '''
print('clicked') @Slot()
def on_press():
''' Tell when the button is pressed. '''
print('pressed') @Slot()
def on_release():
''' Tell when the button is released. '''
print('released') # Connect the signals to the slots
btn.clicked.connect(on_click)
btn.pressed.connect(on_press)
btn.released.connect(on_release) # Show the window and run the app
win.show()
app.exec_()

当你点击应用的按钮时,它会输出

pressed
released
clicked

pressed信号是按钮被按下时发出,released信号在按钮释放时发出,最后,所有动作完成后,clicked信号被发出。

完成我们的例子程序

现在,很容易完成上一个教程创建的例子程序了。我们为LayoutExample类添加一个显示问候信息的槽方法。

@Slot()
def show_greeting(self):
self.greeting.setText('%s, %s!' %
(self.salutations[self.salutation.currentIndex()],
self.recipient.text()))

我们使用recipient QLineEdit的text()方法来取回用户输入的文本,salutation QComboBox的currentIndex()方法获得用户的选择。这里同样使用Slot()修饰符来表明show_greeting将被作为槽来使用。然后,我们将按钮的clicked信号与之连接:

self.build_button.clicked.connect(self.show_greeting)

最后,例子像是这样:

 import sys
from PySide.QtCore import Slot
from PySide.QtGui import * # Every Qt application must have one and only one QApplication object;
# it receives the command line arguments passed to the script, as they
# can be used to customize the application's appearance and behavior
qt_app = QApplication(sys.argv) class LayoutExample(QWidget):
''' An example of PySide absolute positioning; the main window
inherits from QWidget, a convenient widget for an empty window. ''' def __init__(self):
# Initialize the object as a QWidget and
# set its title and minimum width
QWidget.__init__(self)
self.setWindowTitle('Dynamic Greeter')
self.setMinimumWidth(400) # Create the QVBoxLayout that lays out the whole form
self.layout = QVBoxLayout() # Create the form layout that manages the labeled controls
self.form_layout = QFormLayout() self.salutations = ['Ahoy',
'Good day',
'Hello',
'Heyo',
'Hi',
'Salutations',
'Wassup',
'Yo'] # Create and fill the combo box to choose the salutation
self.salutation = QComboBox(self)
self.salutation.addItems(self.salutations)
# Add it to the form layout with a label
self.form_layout.addRow('&Salutation:', self.salutation) # Create the entry control to specify a
# recipient and set its placeholder text
self.recipient = QLineEdit(self)
self.recipient.setPlaceholderText("e.g. 'world' or 'Matey'") # Add it to the form layout with a label
self.form_layout.addRow('&Recipient:', self.recipient) # Create and add the label to show the greeting text
self.greeting = QLabel('', self)
self.form_layout.addRow('Greeting:', self.greeting) # Add the form layout to the main VBox layout
self.layout.addLayout(self.form_layout) # Add stretch to separate the form layout from the button
self.layout.addStretch(1) # Create a horizontal box layout to hold the button
self.button_box = QHBoxLayout() # Add stretch to push the button to the far right
self.button_box.addStretch(1) # Create the build button with its caption
self.build_button = QPushButton('&Build Greeting', self) # Connect the button's clicked signal to show_greeting
self.build_button.clicked.connect(self.show_greeting) # Add it to the button box
self.button_box.addWidget(self.build_button) # Add the button box to the bottom of the main VBox layout
self.layout.addLayout(self.button_box) # Set the VBox layout as the window's main layout
self.setLayout(self.layout) @Slot()
def show_greeting(self):
''' Show the constructed greeting. '''
self.greeting.setText('%s, %s!' %
(self.salutations[self.salutation.currentIndex()],
self.recipient.text())) def run(self):
# Show the form
self.show()
# Run the qt application
qt_app.exec_() # Create an instance of the application window and run it
app = LayoutExample()
app.run()

运行它你会发现点击按钮可以产生问候信息了。现在我们知道了如何使用我们创建的槽去连接内建的信号,下一个教程中,我们将学习创建并连接自己的信号。

By Ascii0x03

转载请注明出处:http://www.cnblogs.com/ascii0x03/p/5499507.html

PySide——Python图形化界面入门教程(三)的更多相关文章

  1. PySide——Python图形化界面入门教程(二)

    PySide——Python图形化界面入门教程(二) ——交互Widget和布局容器 ——Interactive Widgets and Layout Containers 翻译自:http://py ...

  2. PySide——Python图形化界面入门教程(四)

    PySide——Python图形化界面入门教程(四) ——创建自己的信号槽 ——Creating Your Own Signals and Slots 翻译自:http://pythoncentral ...

  3. PySide——Python图形化界面入门教程(六)

    PySide——Python图形化界面入门教程(六) ——QListView和QStandardItemModel 翻译自:http://pythoncentral.io/pyside-pyqt-tu ...

  4. PySide——Python图形化界面入门教程(五)

    PySide——Python图形化界面入门教程(五) ——QListWidget 翻译自:http://pythoncentral.io/pyside-pyqt-tutorial-the-qlistw ...

  5. PySide——Python图形化界面入门教程(一)

    PySide——Python图形化界面入门教程(一) ——基本部件和HelloWorld 翻译自:http://pythoncentral.io/intro-to-pysidepyqt-basic-w ...

  6. PySide——Python图形化界面

    PySide——Python图形化界面 PySide——Python图形化界面入门教程(四) PySide——Python图形化界面入门教程(四) ——创建自己的信号槽 ——Creating Your ...

  7. python+pycharm+PyQt5 图形化界面安装教程

    python图形化界面安装教程 配置环境变量 主目录 pip所在目录,及script目录 更新pip(可选) python -m pip install --upgrade pip ps:更新出错一般 ...

  8. Oracle数据库及图形化界面安装教程详解

    百度云盘oracle数据库及图形化界面安装包 链接: https://pan.baidu.com/s/1DHfui-D2n1R6_ND3wDziQw 密码: f934 首先在电脑D盘(或者其他不是C盘 ...

  9. 如何使用python图形化界面wxPython

    GUI库主要有三类:tkinter,wxPython和PyQt5,下面主要是针对wxPython的使用说明. 下面的操作均在win10 + pycharm上进行 wxPython的安装: pip in ...

随机推荐

  1. 忙里偷闲( ˇˍˇ )闲里偷学【C语言篇】——(7)结构体

    一.为什么需要结构体? 为了表示一些复杂的事物,而普通类型无法满足实际需求 二.什么叫结构体? 把一些基本类型组合在一起形成的一个新的复合数据类型叫做结构体. 三.如何定义一个结构体? 第一种方式: ...

  2. C++ 指向类成员函数指针的用法(转自维基百科)

    类成员函数指针 类成员函数指针(member function pointer),是C++语言的一类指针数据类型,用于存储一个指定类具有给定的形参列表与返回值类型的成员函数的访问信息. 目录 1 语法 ...

  3. while 常见程序逻辑

    1. 查找 List L; Position P = L; while (P && P->Element != Key) { P = P->Next; } return P ...

  4. asp.net core2.1 部署centos7/linux系统 -- 安装部署(一)

    原文:asp.net core2.1 部署centos7/linux系统 -- 安装部署(一) 1.安装dotnet sdk(添加产品秘钥与yum源) 添加yum源:sudo rpm -Uvh htt ...

  5. iOS常用加密方法(aes、md5、base64)

    1.代码 iOS常用加密方法(aes.md5.base64) .AES加密 NSData+AES.h文件 // // NSData-AES.h // Smile // // Created by 周 ...

  6. 【前端统计图】echarts改变颜色属性的demo

    一:柱状图改变颜色 图片.png 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8&qu ...

  7. spark accumulator累加器

    java /** * accumulator可以让多个task共同操作一份变量,主要进行多个节点对一个变量进行共享性的操作,accumulator只提供了累加的功能 * 只有driver可以获取acc ...

  8. SpringBoot集成PageHelper时出现“在系统中发现了多个分页插件,请检查系统配置!”

    近日在项目中使用SpringBoot集成PageHelper后,跑单元测试时出现了"在系统中发现了多个分页插件,请检查系统配置!"这个问题. 如下图所示: org.mybatis. ...

  9. 一种基于uCos-II操作系统和lwIP协议栈的IEEE-1588主站以及基于该主站的报文处理方法

    主站以及应用于电力系统的支持IEEE‐1588协议的主时钟(IEEE‐1588主站)的实现方法.该方法是在一个低成本的硬件平台上,借助uCos‐II操作系统和TCP/IP的协议栈,对以太网数据进行了分 ...

  10. 生成式模型(generative) vs 判别式模型(discriminative)

    Andrew Ng, On Discriminative vs. Generative classifiers: A comparison of logistic regression and nai ...