| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- import sys
- from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QPushButton, QInputDialog, QWidget, QLineEdit
- class MainWindow(QMainWindow):
- def __init__(self):
- super().__init__()
- self.setWindowTitle("My App")
- layout = QVBoxLayout()
- button1 = QPushButton("Integer")
- button1.clicked.connect(self.get_an_int)
- layout.addWidget(button1)
- button2 = QPushButton("Float")
- button2.clicked.connect(self.get_a_float)
- layout.addWidget(button2)
- button3 = QPushButton("Select")
- button3.clicked.connect(self.get_a_string_from_a_list)
- layout.addWidget(button3)
- button4 = QPushButton("String")
- button4.clicked.connect(self.get_a_str)
- layout.addWidget(button4)
- button5 = QPushButton("Text")
- button5.clicked.connect(self.get_a_text)
- layout.addWidget(button5)
- container = QWidget()
- container.setLayout(layout)
- self.setCentralWidget(container)
- def get_an_int(self):
- title = "Enter an interger"
- label = "Please enter an integer"
- my_int_value, ok = QInputDialog.getInt(self,
- title,
- label,
- value=0,
- min=5,
- max=50,
- step=1)
- print("Result:", ok, my_int_value)
- def get_a_float(self):
- title = "Enter a float"
- label = "Please enter a float"
- my_float_value, ok = QInputDialog.getDouble(self,
- title,
- label,
- value=0.0,
- min=-5.3,
- max=10.0,
- decimals=2)
- print("Result:", ok, my_float_value)
- def get_a_string_from_a_list(self):
- title = "Select an item"
- label = "Please select an item"
- items = ["apple", "pear", "orange", "grape"]
- initail_selection = 2 # index of orange
- item, ok = QInputDialog.getItem(self, title, label, items,
- initail_selection, False)
- print("Result:", ok, item)
- def get_a_str(self):
- title = "Enter a string"
- label = "Type your password"
- text = "my secret password"
- my_str_value, ok = QInputDialog.getText(self, title, label,
- QLineEdit.Password, text)
- print("Result:", ok, my_str_value)
- def get_a_text(self):
- title = "Enter a text"
- label = "Type your message"
- text = "Hello, World! ..."
- my_text_value, ok = QInputDialog.getMultiLineText(
- self, title, label, text)
- print("Result:", ok, my_text_value)
- if __name__ == "__main__":
- app = QApplication(sys.argv)
- window = MainWindow()
- window.show()
- app.exec_()
|