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()
運行它你會發現點擊按鈕可以產生問候信息了。現在我們知道了如何使用我們創建的槽去連接內建的信號,下一個教程中,我們將學習創建並連接自己的信號。
By Ascii0x03
轉載請注明出處:http://www.cnblogs.com/ascii0x03/p/5499507.html