dialogs_input_1.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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,
  30. title,
  31. label,
  32. value=0,
  33. min=5,
  34. max=50,
  35. step=1)
  36. print("Result:", ok, my_int_value)
  37. def get_a_float(self):
  38. title = "Enter a float"
  39. label = "Please enter a float"
  40. my_float_value, ok = QInputDialog.getDouble(self,
  41. title,
  42. label,
  43. value=0.0,
  44. min=-5.3,
  45. max=10.0,
  46. decimals=2)
  47. print("Result:", ok, my_float_value)
  48. def get_a_string_from_a_list(self):
  49. title = "Select an item"
  50. label = "Please select an item"
  51. items = ["apple", "pear", "orange", "grape"]
  52. initail_selection = 2 # index of orange
  53. item, ok = QInputDialog.getItem(self, title, label, items,
  54. initail_selection, False)
  55. print("Result:", ok, item)
  56. def get_a_str(self):
  57. title = "Enter a string"
  58. label = "Type your password"
  59. text = "my secret password"
  60. my_str_value, ok = QInputDialog.getText(self, title, label,
  61. QLineEdit.Password, text)
  62. print("Result:", ok, my_str_value)
  63. def get_a_text(self):
  64. title = "Enter a text"
  65. label = "Type your message"
  66. text = "Hello, World! ..."
  67. my_text_value, ok = QInputDialog.getMultiLineText(
  68. self, title, label, text)
  69. print("Result:", ok, my_text_value)
  70. if __name__ == "__main__":
  71. app = QApplication(sys.argv)
  72. window = MainWindow()
  73. window.show()
  74. app.exec_()