from PySide.QtGui import QDialog, QLabel, QPushButton, QVBoxLayout, QApplication class MyDialog(QDialog): def __init__(self, parent=None): super(MyDialog, self).__init__(parent) label = QLabel("Hello, world!") button = QPushButton("OK") button.clicked.connect(self.accept) layout = QVBoxLayout() layout.addWidget(label) layout.addWidget(button) self.setLayout(layout) if __name__ == '__main__': app = QApplication([]) dialog = MyDialog() dialog.exec_()
from PySide.QtGui import QDialog, QLabel, QPushButton, QVBoxLayout, QApplication, QFileDialog class MyDialog(QDialog): def __init__(self, parent=None): super(MyDialog, self).__init__(parent) label = QLabel("Select a file to open:") button = QPushButton("OK") button.clicked.connect(self.select_file) layout = QVBoxLayout() layout.addWidget(label) layout.addWidget(button) self.setLayout(layout) def select_file(self): filename, _ = QFileDialog.getOpenFileName(self, "Open File", "", "All Files (*.*)") print("Selected file:", filename) self.accept() if __name__ == '__main__': app = QApplication([]) dialog = MyDialog() dialog.exec_()In both examples, we import the PySide.QtGui module and use the QDialog class to create our dialog box. We then add widgets to the dialog box using a layout. In the first example, we add a QLabel and QPushButton to the QVBoxLayout layout. In the second example, we add a QLabel and QPushButton and connect the button to a method that uses the QFileDialog class to select a file. Finally, we use the exec_() method to display the dialog box.