dialogs_input_1.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import sys
  2. from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QPushButton, QInputDialog, QWidget, QLineEdit
  3. class MainWindow(QMainWindow):
  4. def __init__(self):
  5. super().__init__()
  6. self.setWindowTitle("My App")
  7. layout = QVBoxLayout()
  8. button1 = QPushButton("Integer")
  9. button1.clicked.connect(self.get_an_int)
  10. layout.addWidget(button1)
  11. button2 = QPushButton("Float")
  12. button2.clicked.connect(self.get_a_float)
  13. layout.addWidget(button2)
  14. button3 = QPushButton("Select")
  15. button3.clicked.connect(self.get_a_string_from_a_list)
  16. layout.addWidget(button3)
  17. button4 = QPushButton("String")
  18. button4.clicked.connect(self.get_a_str)
  19. layout.addWidget(button4)
  20. button5 = QPushButton("Text")
  21. button5.clicked.connect(self.get_a_text)
  22. layout.addWidget(button5)
  23. container = QWidget()
  24. container.setLayout(layout)
  25. self.setCentralWidget(container)
  26. def get_an_int(self):
  27. title = "Enter an interger"
  28. label = "Please enter an integer"
  29. my_int_value, ok = QInputDialog.getInt(self, title, label, value=0, min=5, max=50,step=1)
  30. print("Result:", ok, my_int_value)
  31. def get_a_float(self):
  32. title = "Enter a float"
  33. label = "Please enter a float"
  34. my_float_value, ok = QInputDialog.getDouble(self, title, label, value=0.0, min=-5.3, max=10.0, decimals=2)
  35. print("Result:", ok, my_float_value)
  36. def get_a_string_from_a_list(self):
  37. title = "Select an item"
  38. label = "Please select an item"
  39. items = ["apple", "pear", "orange", "grape"]
  40. initail_selection = 2 # index of orange
  41. item, ok = QInputDialog.getItem(self, title, label, items, initail_selection, False)
  42. print("Result:", ok, item)
  43. def get_a_str(self):
  44. title = "Enter a string"
  45. label = "Type your password"
  46. text = "my secret password"
  47. my_str_value, ok = QInputDialog.getText(self, title, label, QLineEdit.Password, text)
  48. print("Result:", ok, my_str_value)
  49. def get_a_text(self):
  50. title = "Enter a text"
  51. label = "Type your message"
  52. text = "Hello, World! ..."
  53. my_text_value, ok = QInputDialog.getMultiLineText(self, title, label, text)
  54. print("Result:", ok, my_text_value)
  55. if __name__ == "__main__":
  56. app = QApplication(sys.argv)
  57. window = MainWindow()
  58. window.show()
  59. app.exec_()