PySide——Python图形化界面入门教程(三)
——使用内建新号和槽
——Using Built-In Signals and Slots
上一个教程中,我们学习了如何创建和建立交互widgets,以及将他们布局的两种不同的方法。今天我们继续讨论Python/Qt应用响应用户触发的事件:信号和槽。
当用户执行一个动作——点击按钮,选择组合框的值,在文本框中打字——这个widget就会发出一个信号。这个信号自己什么都不做,它必须和槽连接起来才行。槽是一个接受信号的执行动作的对象。
连接内建PySide/PyQt信号
Qt widgets有许多的内建信号。例如,当QPushButton被点击的时候,它发出它的clicked信号。clicked信号可以被连接到一个拥有槽功能的函数(只是一个概要,需要更多内容去运行)
1 @Slot()
2 def clicked_slot():
3 ''' This is called when the button is clicked. '''
4 print('Ouch!')
5
6
7 # Create the button
8 btn = QPushButton('Sample')
9
10 # Connect its clicked signal to our slot
11 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。
1 import sys
2 from PySide.QtCore import Slot
3 from PySide.QtGui import *
4
5 # ... insert the rest of the imports here
6 # Imports must precede all others ...
7
8 # Create a Qt app and a window
9 app = QApplication(sys.argv)
10
11 win = QWidget()
12 win.setWindowTitle('Test Window')
13
14 # Create a button in the window
15 btn = QPushButton('Test', win)
16
17 @Slot()
18 def on_click():
19 ''' Tell when the button is clicked. '''
20 print('clicked')
21
22 @Slot()
23 def on_press():
24 ''' Tell when the button is pressed. '''
25 print('pressed')
26
27 @Slot()
28 def on_release():
29 ''' Tell when the button is released. '''
30 print('released')
31
32 # Connect the signals to the slots
33 btn.clicked.connect(on_click)
34 btn.pressed.connect(on_press)
35 btn.released.connect(on_release)
36
37 # Show the window and run the app
38 win.show()
39 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)
最后,例子像是这样:
1 import sys
2 from PySide.QtCore import Slot
3 from PySide.QtGui import *
4
5 # Every Qt application must have one and only one QApplication object;
6 # it receives the command line arguments passed to the script, as they
7 # can be used to customize the application's appearance and behavior
8 qt_app = QApplication(sys.argv)
9
10 class LayoutExample(QWidget):
11 ''' An example of PySide absolute positioning; the main window
12 inherits from QWidget, a convenient widget for an empty window. '''
13
14 def __init__(self):
15 # Initialize the object as a QWidget and
16 # set its title and minimum width
17 QWidget.__init__(self)
18 self.setWindowTitle('Dynamic Greeter')
19 self.setMinimumWidth(400)
20
21 # Create the QVBoxLayout that lays out the whole form
22 self.layout = QVBoxLayout()
23
24 # Create the form layout that manages the labeled controls
25 self.form_layout = QFormLayout()
26
27 self.salutations = ['Ahoy',
28 'Good day',
29 'Hello',
30 'Heyo',
31 'Hi',
32 'Salutations',
33 'Wassup',
34 'Yo']
35
36 # Create and fill the combo box to choose the salutation
37 self.salutation = QComboBox(self)
38 self.salutation.addItems(self.salutations)
39 # Add it to the form layout with a label
40 self.form_layout.addRow('&Salutation:', self.salutation)
41
42 # Create the entry control to specify a
43 # recipient and set its placeholder text
44 self.recipient = QLineEdit(self)
45 self.recipient.setPlaceholderText("e.g. 'world' or 'Matey'")
46
47 # Add it to the form layout with a label
48 self.form_layout.addRow('&Recipient:', self.recipient)
49
50 # Create and add the label to show the greeting text
51 self.greeting = QLabel('', self)
52 self.form_layout.addRow('Greeting:', self.greeting)
53
54 # Add the form layout to the main VBox layout
55 self.layout.addLayout(self.form_layout)
56
57 # Add stretch to separate the form layout from the button
58 self.layout.addStretch(1)
59
60 # Create a horizontal box layout to hold the button
61 self.button_box = QHBoxLayout()
62
63 # Add stretch to push the button to the far right
64 self.button_box.addStretch(1)
65
66 # Create the build button with its caption
67 self.build_button = QPushButton('&Build Greeting', self)
68
69 # Connect the button's clicked signal to show_greeting
70 self.build_button.clicked.connect(self.show_greeting)
71
72 # Add it to the button box
73 self.button_box.addWidget(self.build_button)
74
75 # Add the button box to the bottom of the main VBox layout
76 self.layout.addLayout(self.button_box)
77
78 # Set the VBox layout as the window's main layout
79 self.setLayout(self.layout)
80
81 @Slot()
82 def show_greeting(self):
83 ''' Show the constructed greeting. '''
84 self.greeting.setText('%s, %s!' %
85 (self.salutations[self.salutation.currentIndex()],
86 self.recipient.text()))
87
88 def run(self):
89 # Show the form
90 self.show()
91 # Run the qt application
92 qt_app.exec_()
93
94 # Create an instance of the application window and run it
95 app = LayoutExample()
96 app.run()
运行它你会发现点击按钮可以产生问候信息了。现在我们知道了如何使用我们创建的槽去连接内建的信号,下一个教程中,我们将学习创建并连接自己的信号。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有