| 112 | 7. 使用 layout 來排列 widgets: |
| 113 | {{{ |
| 114 | #!python |
| 115 | #!/usr/bin/python |
| 116 | # -*- coding: utf-8 -*- |
| 117 | # Import PySide classes |
| 118 | import sys |
| 119 | from PySide.QtCore import * |
| 120 | from PySide.QtGui import * |
| 121 | |
| 122 | |
| 123 | |
| 124 | class Form(QDialog): |
| 125 | def __init__(self, parent=None): |
| 126 | super(Form, self).__init__(parent) |
| 127 | # 建立 widgets |
| 128 | self.edit = QLineEdit('input your name') |
| 129 | self.button = QPushButton('submit') |
| 130 | |
| 131 | # 建立 layout 並加入上面的 widgets |
| 132 | layout = QVBoxLayout() |
| 133 | layout.addWidget(self.edit) |
| 134 | layout.addWidget(self.button) |
| 135 | # 設定 dialog layout |
| 136 | self.setLayout(layout) |
| 137 | # 設定 dialog 的設明 |
| 138 | self.setWindowTitle('My form') |
| 139 | |
| 140 | # 將 button 事件與 say_hellow slot 連結 |
| 141 | self.button.clicked.connect(self.say_hellow) |
| 142 | |
| 143 | def say_hellow(self): |
| 144 | print ("Hello %s" % self.edit.text()) |
| 145 | |
| 146 | |
| 147 | |
| 148 | if __name__ == '__main__': |
| 149 | # 建立 QT 應用程式 |
| 150 | app = QApplication(sys.argv) |
| 151 | # 建立並顯示 form |
| 152 | form = Form() |
| 153 | form.show() |
| 154 | # 執行主要的 QT loop |
| 155 | sys.exit(app.exec_()) |
| 156 | |
| 157 | }}} |
| 158 | |
| 159 | |