Bladeren bron

basic features

simon 10 maanden geleden
bovenliggende
commit
014f68cc99
11 gewijzigde bestanden met toevoegingen van 491 en 9 verwijderingen
  1. 20 0
      basic/dialogs_2a.py
  2. 33 0
      basic/dialogs_3.py
  3. 39 0
      basic/dialogs_5.py
  4. 78 0
      basic/dialogs_input_1.py
  5. 31 0
      basic/dialogs_start.py
  6. 93 0
      basic/layout_10.py
  7. 30 9
      basic/layout_8.py
  8. 27 0
      basic/layout_9.py
  9. 49 0
      basic/windows_1.py
  10. 46 0
      basic/windows_5.py
  11. 45 0
      basic/windows_7.py

+ 20 - 0
basic/dialogs_2a.py

@@ -0,0 +1,20 @@
+from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QVBoxLayout, QLabel
+
+
+class CustomDialog(QDialog):
+    def __init__(self, parent=None):
+        super().__init__(parent)
+
+        self.setWindowTitle("Hello!")
+
+        buttons = QDialogButtonBox.Ok | QDialogButtonBox.Cancel
+
+        self.buttonBox = QDialogButtonBox(buttons)
+        self.buttonBox.accepted.connect(self.accepted)
+        self.buttonBox.rejected.connect(self.rejected)
+
+        self.layout = QVBoxLayout()
+        message = QLabel("Something happened, is that OK?")
+        self.layout.addWidget(message)
+        self.layout.addWidget(self.buttonBox)
+        self.setLayout(self.layout)

+ 33 - 0
basic/dialogs_3.py

@@ -0,0 +1,33 @@
+import sys
+
+from PyQt5.QtWidgets import QMainWindow, QPushButton, QMessageBox, QApplication
+
+
+class MainWindow(QMainWindow):
+    def __init__(self):
+        super().__init__()
+        self.setWindowTitle('My App')
+
+        button = QPushButton('Press me for a dialog!')
+        button.setCheckable(True)
+        button.clicked.connect(self.button_clicked)
+        self.setCentralWidget(button)
+
+    def button_clicked(self, is_checked):
+        dlg = QMessageBox(self)
+        dlg.setIcon(QMessageBox.Question)
+        dlg.setWindowTitle('I have a question!')
+        dlg.setText('This is a simple dialog')
+        dlg.setStandardButtons(QMessageBox.No | QMessageBox.Yes)
+        button = dlg.exec_()
+
+        if button == QMessageBox.Yes:
+            print("YES!")
+        else:
+            print("NO!")
+
+
+app = QApplication(sys.argv)
+window = MainWindow()
+window.show()
+sys.exit(app.exec_())

+ 39 - 0
basic/dialogs_5.py

@@ -0,0 +1,39 @@
+import sys
+
+from PyQt5.QtWidgets import QMainWindow, QPushButton, QMessageBox, QApplication
+
+
+class MainWindow(QMainWindow):
+    def __init__(self):
+        super().__init__()
+        self.setWindowTitle('My App')
+
+        button = QPushButton('Press me for a dialog!')
+        button.setCheckable(True)
+        button.clicked.connect(self.button_clicked)
+        self.setCentralWidget(button)
+
+    def button_clicked(self, is_checked):
+        # button = QMessageBox.question(self, "Question dialog", "The longer message")
+        button = QMessageBox.critical(
+            self,
+            'Oh dear!',
+            'Somthing went very wrong',
+            buttons=QMessageBox.Discard
+                    | QMessageBox.NoToAll
+                    | QMessageBox.Ignore,
+            defaultButton=QMessageBox.Discard,
+        )
+
+        if button == QMessageBox.Discard:
+            print("Discard!")
+        elif button == QMessageBox.NoToAll:
+            print("No to all!")
+        else:
+            print("Ignore!")
+
+
+app = QApplication(sys.argv)
+window = MainWindow()
+window.show()
+sys.exit(app.exec_())

+ 78 - 0
basic/dialogs_input_1.py

@@ -0,0 +1,78 @@
+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_()

+ 31 - 0
basic/dialogs_start.py

@@ -0,0 +1,31 @@
+import sys
+
+from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication
+
+from dialogs_2a import CustomDialog
+
+
+class MainWindow(QMainWindow):
+    def __init__(self):
+        super().__init__()
+
+        self.setWindowTitle("My First Window")
+
+        button = QPushButton("Press me for a dialog!")
+        button.clicked.connect(self.button_clicked)
+        self.setCentralWidget(button)
+
+    def button_clicked(self, is_checked):
+        print("click", is_checked)
+
+        dlg = CustomDialog(self)
+        if dlg.exec_():
+            print("Success!")
+        else:
+            print("Cancel!")
+
+
+app = QApplication(sys.argv)
+window = MainWindow()
+window.show()
+sys.exit(app.exec_())

+ 93 - 0
basic/layout_10.py

@@ -0,0 +1,93 @@
+import sys
+
+from PyQt5.QtWidgets import (
+    QMainWindow, QFormLayout,
+    QLineEdit, QSpinBox,
+    QComboBox, QWidget,
+    QApplication, QLabel,
+    QAction, QToolBar,
+    QStatusBar,
+)
+
+
+class MyWindow(QMainWindow):
+    def __init__(self):
+        super().__init__()
+        self.setWindowTitle('My App')
+
+        layout = QFormLayout()
+
+        # Dictionary to store the form data
+        self.data = {
+            "name": "Johnina Smith",
+            "age": 10,
+            "icecream": "Vanilla",
+        }
+
+        self.name = QLineEdit()
+        self.name.setText(self.data["name"])
+        self.name.textChanged.connect(self.handle_name_changed)
+        self.age = QSpinBox()
+        self.age.setRange(0, 200)
+        self.age.setValue(self.data["age"])
+        self.age.valueChanged.connect(self.handle_age_changed)
+        self.icecream = QComboBox()
+        self.icecream.addItems(["Vanilla", "Strawberry", "Chocolate"])
+        self.icecream.setCurrentText(self.data["icecream"])
+        self.icecream.currentTextChanged.connect(self.handle_icecream_changed)
+
+        layout.addRow("Name", self.name)
+        layout.addRow("Age", self.age)
+        layout.addRow("Favorite icecream", self.icecream)
+        # Empty label to show error message
+        self.error = QLabel()
+        layout.addWidget(self.error)
+
+        toolbar = QToolBar("My main toolbar")
+
+        widget = QWidget()
+        widget.setLayout(layout)
+        self.setCentralWidget(widget)
+        toolbar.toggleViewAction().setEnabled(False)
+        self.addToolBar(toolbar)
+
+        button_action = QAction("Your button", self)
+        button_action.setStatusTip("This is your button")
+        button_action.triggered.connect(self.onMyToolBarButtonClick)
+        button_action.setCheckable(True)
+        toolbar.addAction(button_action)
+
+        self.setStatusBar(QStatusBar(self))
+
+    def handle_name_changed(self, name):
+        self.data["name"] = name
+        print(self.data)
+        self.validate()
+
+    def handle_age_changed(self, age):
+        self.data["age"] = age
+        print(self.data)
+        self.validate()
+
+    def handle_icecream_changed(self, icecream):
+        self.data["icecream"] = icecream
+        print(self.data)
+        self.validate()
+
+    def validate(self):
+        if self.data["age"] > 10 and self.data["icecream"] == "Chocolate":
+            self.error.setText("People over 10 aren't allowed chocolate ice cream")
+            return
+        if self.data["age"] > 100:
+            self.error.setText("Did you send a telegram?")
+            return
+        self.error.setText("")
+
+    def onMyToolBarButtonClick(self, is_checked):
+        print("click", is_checked)
+
+
+app = QApplication(sys.argv)
+window = MyWindow()
+window.show()
+sys.exit(app.exec_())

+ 30 - 9
basic/layout_8.py

@@ -1,6 +1,6 @@
 import sys
 
-from PyQt5.QtWidgets import QMainWindow, QStackedLayout, QWidget, QApplication
+from PyQt5.QtWidgets import QMainWindow, QStackedLayout, QWidget, QApplication, QVBoxLayout, QHBoxLayout, QPushButton
 
 from layout_colorwidget import Color
 
@@ -10,20 +10,41 @@ class MainWindow(QMainWindow):
         super().__init__()
         self.setWindowTitle("My App")
 
-        layout = QStackedLayout()
+        page_layout = QVBoxLayout()
+        button_layout = QHBoxLayout()
+        self.stack_layout = QStackedLayout()
 
-        layout.addWidget(Color("red"))
-        layout.addWidget(Color("gree"))
-        layout.addWidget(Color("blue"))
-        layout.addWidget(Color("purple"))
-        layout.addWidget(Color("yellow"))
+        page_layout.addLayout(self.stack_layout)
+        page_layout.addLayout(button_layout)
 
-        layout.setCurrentIndex(4)
+        btn = QPushButton("red")
+        btn.pressed.connect(self.activate_tab_1)
+        button_layout.addWidget(btn)
+        self.stack_layout.addWidget(Color("red"))
+
+        btn = QPushButton("green")
+        btn.pressed.connect(self.activate_tab_2)
+        button_layout.addWidget(btn)
+        self.stack_layout.addWidget(Color("green"))
+
+        btn = QPushButton("yellow")
+        btn.pressed.connect(self.activate_tab_3)
+        button_layout.addWidget(btn)
+        self.stack_layout.addWidget(Color("yellow"))
 
         widget = QWidget()
-        widget.setLayout(layout)
+        widget.setLayout(page_layout)
         self.setCentralWidget(widget)
 
+    def activate_tab_1(self):
+        self.stack_layout.setCurrentIndex(0)
+
+    def activate_tab_2(self):
+        self.stack_layout.setCurrentIndex(1)
+
+    def activate_tab_3(self):
+        self.stack_layout.setCurrentIndex(2)
+
 
 app = QApplication(sys.argv)
 window = MainWindow()

+ 27 - 0
basic/layout_9.py

@@ -0,0 +1,27 @@
+import sys
+
+from PyQt5.QtWidgets import QMainWindow, QTabWidget, QApplication
+
+from layout_colorwidget import Color
+
+
+class MainWindow(QMainWindow):
+    def __init__(self):
+        super().__init__()
+        self.setWindowTitle("My App")
+
+        tabs = QTabWidget()
+        tabs.setDocumentMode(True)
+        tabs.setTabPosition(QTabWidget.North)
+        tabs.setMovable(True)
+
+        for color in ["red", "green", "blue", "yellow"]:
+            tabs.addTab(Color(color), color)
+
+        self.setCentralWidget(tabs)
+
+
+app = QApplication(sys.argv)
+window = MainWindow()
+window.show()
+sys.exit(app.exec_())

+ 49 - 0
basic/windows_1.py

@@ -0,0 +1,49 @@
+import sys 
+from random import randint
+from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QPushButton, QWidget, QInputDialog, QLineEdit, QLabel
+
+class AnotherWindow(QWidget):
+    """
+    This "window" is a Qwidget. If it has no parent, it 
+    will appear as a free-floating window.
+    """
+    def __init__(self):
+        super().__init__()
+        layout = QVBoxLayout()
+        self.label = QLabel("Another Window %d" % randint(0,100))
+        layout.addWidget(self.label)
+        self.setLayout(layout)
+
+
+class MainWindow(QMainWindow):
+    def __init__(self):
+        super().__init__()
+        self.w = None # No external window yet
+
+        self.setWindowTitle("My App")
+
+        layout = QVBoxLayout()
+    
+        self.button = QPushButton("Press me for a dialog!")
+        self.button.clicked.connect(self.show_new_window)
+        layout.addWidget(self.button)
+
+        container = QWidget()
+        container.setLayout(layout)
+        
+        self.setCentralWidget(container)
+
+    def show_new_window(self, is_checked):
+        print("Button clicked!", is_checked)
+        if self.w is None:
+            self.w = AnotherWindow()
+            self.w.show()
+        else:
+            self.w.close()
+            self.w = None   # Discard reference, close window.
+
+
+app = QApplication(sys.argv)
+window = MainWindow()
+window.show()
+app.exec_()

+ 46 - 0
basic/windows_5.py

@@ -0,0 +1,46 @@
+import sys 
+from random import randint
+from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QPushButton, QWidget, QInputDialog, QLineEdit, QLabel
+
+class AnotherWindow(QWidget):
+    """
+    This "window" is a Qwidget. If it has no parent, it 
+    will appear as a free-floating window.
+    """
+    def __init__(self):
+        super().__init__()
+        layout = QVBoxLayout()
+        self.label = QLabel("Another Window %d" % randint(0,100))
+        layout.addWidget(self.label)
+        self.setLayout(layout)
+
+
+class MainWindow(QMainWindow):
+    def __init__(self):
+        super().__init__()
+        self.w = AnotherWindow()
+
+        self.setWindowTitle("My App")
+
+        layout = QVBoxLayout()
+    
+        self.button = QPushButton("Press me for a dialog!")
+        self.button.clicked.connect(self.show_new_window)
+        layout.addWidget(self.button)
+
+        container = QWidget()
+        container.setLayout(layout)
+        
+        self.setCentralWidget(container)
+
+    def show_new_window(self, is_checked):
+        if self.w.isVisible():
+            self.w.hide()
+        else:
+            self.w.show()
+
+
+app = QApplication(sys.argv)
+window = MainWindow()
+window.show()
+app.exec_()

+ 45 - 0
basic/windows_7.py

@@ -0,0 +1,45 @@
+import sys 
+from random import randint
+from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QPushButton, QWidget, QInputDialog, QLineEdit, QLabel
+
+class AnotherWindow(QWidget):
+    """
+    This "window" is a Qwidget. If it has no parent, it 
+    will appear as a free-floating window.
+    """
+    def __init__(self):
+        super().__init__()
+        layout = QVBoxLayout()
+        self.label = QLabel("Another Window %d" % randint(0,100))
+        layout.addWidget(self.label)
+        self.setLayout(layout)
+
+
+class MainWindow(QMainWindow):
+    def __init__(self):
+        super().__init__()
+        self.w = AnotherWindow()
+        self.button = QPushButton("Press me fo Window!")
+        self.button.clicked.connect(self.toggle_window)
+        self.input = QLineEdit()
+        self.input.textChanged.connect(self.w.label.setText)
+
+        layout = QVBoxLayout()
+        layout.addWidget(self.button)
+        layout.addWidget(self.input)
+
+        container = QWidget()
+        container.setLayout(layout)
+        self.setCentralWidget(container)
+
+    def toggle_window(self, is_checked):
+        if self.w.isVisible():
+            self.w.hide()
+        else:
+            self.w.show()
+
+
+app = QApplication(sys.argv)
+window = MainWindow()
+window.show()
+app.exec_()