# Generated by ChatGPT
import sys
from PyQt5.QtCore import Qt
def __init__(self):
super().__init__()
self.setWindowTitle("PyQt5 Widgets Example")
self.setGeometry(100, 100, 400, 300)
# Create layouts
main_layout
= QVBoxLayout() # Vertical layout for the whole window
# Add a QLabel and QLineEdit (text input)
self.
label = QLabel("Enter your name:", self
)
form_layout.addWidget(self.label)
form_layout.addWidget(self.text_input)
# Add a QComboBox (dropdown)
self.
combo_label = QLabel("Select your favorite color:", self
) self.combo_box.addItems(["Red", "Green", "Blue", "Yellow"])
form_layout.addWidget(self.combo_label)
form_layout.addWidget(self.combo_box)
# Add a QCheckBox
self.
check_box = QCheckBox("I agree to the terms and conditions", self
)
form_layout.addWidget(self.check_box)
# Add a QRadioButton (grouped)
self.
radio_label = QLabel("Choose your preferred language:", self
)
# Add the radio buttons to a horizontal layout
radio_layout.addWidget(self.radio_button_english)
radio_layout.addWidget(self.radio_button_french)
radio_layout.addWidget(self.radio_button_spanish)
form_layout.addWidget(self.radio_label)
form_layout.addLayout(radio_layout)
# Add a QPushButton
self.submit_button.clicked.connect(self.on_button_click)
# Add an exit button
button_layout.addWidget(self.submit_button)
button_layout.addWidget(self.exit_button)
# Add the form layout to the main layout
main_layout.addLayout(form_layout)
main_layout.addLayout(button_layout)
# Set the window layout
self.setLayout(main_layout)
def on_button_click(self):
name = self.text_input.text()
favorite_color = self.combo_box.currentText()
is_agree = self.check_box.isChecked()
preferred_language = "None"
if self.radio_button_english.isChecked():
preferred_language = "English"
elif self.radio_button_french.isChecked():
preferred_language = "French"
elif self.radio_button_spanish.isChecked():
preferred_language = "Spanish"
print(f"Name: {name}")
print(f"Favorite Color: {favorite_color}")
print(f"Agreed to Terms: {is_agree}")
print(f"Preferred Language: {preferred_language}")
if __name__ == "__main__":
window = MyWindow()
window.show()
sys.exit(app.exec_())
# Generated by ChatGPT
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QDialog, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QLineEdit, QComboBox, QCheckBox, QRadioButton
class MyWindow(QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("PyQt5 Widgets Example")
self.setGeometry(100, 100, 400, 300)
# Create layouts
main_layout = QVBoxLayout() # Vertical layout for the whole window
form_layout = QVBoxLayout() # Layout for the form
# Add a QLabel and QLineEdit (text input)
self.label = QLabel("Enter your name:", self)
self.text_input = QLineEdit(self)
form_layout.addWidget(self.label)
form_layout.addWidget(self.text_input)
# Add a QComboBox (dropdown)
self.combo_label = QLabel("Select your favorite color:", self)
self.combo_box = QComboBox(self)
self.combo_box.addItems(["Red", "Green", "Blue", "Yellow"])
form_layout.addWidget(self.combo_label)
form_layout.addWidget(self.combo_box)
# Add a QCheckBox
self.check_box = QCheckBox("I agree to the terms and conditions", self)
form_layout.addWidget(self.check_box)
# Add a QRadioButton (grouped)
self.radio_label = QLabel("Choose your preferred language:", self)
self.radio_button_english = QRadioButton("English", self)
self.radio_button_french = QRadioButton("French", self)
self.radio_button_spanish = QRadioButton("Spanish", self)
# Add the radio buttons to a horizontal layout
radio_layout = QHBoxLayout()
radio_layout.addWidget(self.radio_button_english)
radio_layout.addWidget(self.radio_button_french)
radio_layout.addWidget(self.radio_button_spanish)
form_layout.addWidget(self.radio_label)
form_layout.addLayout(radio_layout)
# Add a QPushButton
self.submit_button = QPushButton("Submit", self)
self.submit_button.clicked.connect(self.on_button_click)
# Add an exit button
self.exit_button = QPushButton("Exit", self)
button_layout = QHBoxLayout()
button_layout.addWidget(self.submit_button)
button_layout.addWidget(self.exit_button)
# Add the form layout to the main layout
main_layout.addLayout(form_layout)
main_layout.addLayout(button_layout)
# Set the window layout
self.setLayout(main_layout)
def on_button_click(self):
name = self.text_input.text()
favorite_color = self.combo_box.currentText()
is_agree = self.check_box.isChecked()
preferred_language = "None"
if self.radio_button_english.isChecked():
preferred_language = "English"
elif self.radio_button_french.isChecked():
preferred_language = "French"
elif self.radio_button_spanish.isChecked():
preferred_language = "Spanish"
print(f"Name: {name}")
print(f"Favorite Color: {favorite_color}")
print(f"Agreed to Terms: {is_agree}")
print(f"Preferred Language: {preferred_language}")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
import QtTheme.PyQt5
from PyQt5.
QtCore import
QFile
def __init__(self):
# ...
qss
= QFile(":/QtTheme/theme/Flat/Dark/Blue/Pink.qss") qss.
open(QFile.
OpenModeFlag.
ReadOnly) self.setStyleSheet(qss.readAll().data().decode())
import QtTheme.PyQt5
from PyQt5.QtCore import QFile
class MyWindow(QDialog):
def __init__(self):
# ...
qss = QFile(":/QtTheme/theme/Flat/Dark/Blue/Pink.qss")
qss.open(QFile.OpenModeFlag.ReadOnly)
self.setStyleSheet(qss.readAll().data().decode())
To copy to clipboard, switch view to plain text mode
def __init__(self):
# ...
qss
= QFile(":/QtTheme/theme/Flat/Dark/Blue/Pink.qss") qss.
open(QFile.
OpenModeFlag.
ReadOnly) self.setStyleSheet(qss.readAll().data().decode())
self.submit_button.setProperty("Color", "Primary")
self.exit_button.setProperty("Color", "Danger")
class MyWindow(QDialog):
def __init__(self):
# ...
qss = QFile(":/QtTheme/theme/Flat/Dark/Blue/Pink.qss")
qss.open(QFile.OpenModeFlag.ReadOnly)
self.setStyleSheet(qss.readAll().data().decode())
self.submit_button.setProperty("Color", "Primary")
self.exit_button.setProperty("Color", "Danger")
To copy to clipboard, switch view to plain text mode
Bookmarks