PyQt 笔记

                     

贡献者: 待更新

  • 本文处于草稿阶段。

   GPT-4 生成的一个程序

图
图 1:可以把输入的字体显示为不同颜色

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, \ 
    QMenu, QMenuBar, QAction, QRadioButton, QVBoxLayout, \ 
    QLabel, QLineEdit, QPushButton, QWidget, QGroupBox
from PyQt5.QtGui import QColor

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):
        # Drop menu
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('File')

        exitAction = QAction('Exit', self)
        exitAction.triggered.connect(self.close)
        fileMenu.addAction(exitAction)

        # Radio buttons (circled single selection)
        self.radio_group = QGroupBox("Color Options")
        self.radio_layout = QVBoxLayout()

        self.red_radio = QRadioButton("Red")
        self.green_radio = QRadioButton("Green")
        self.blue_radio = QRadioButton("Blue")

        self.radio_layout.addWidget(self.red_radio)
        self.radio_layout.addWidget(self.green_radio)
        self.radio_layout.addWidget(self.blue_radio)

        self.radio_group.setLayout(self.radio_layout)

        # Textbox
        self.textbox = QLineEdit(self)
        self.textbox.setPlaceholderText("Enter text here")

        # Button to display the text
        self.button = QPushButton("Display Text", self)
        self.button.clicked.connect(self.on_click)

        # Label to display the text
        self.result_label = QLabel("")

        # Main layout
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.radio_group)
        self.layout.addWidget(self.textbox)
        self.layout.addWidget(self.button)
        self.layout.addWidget(self.result_label)

        self.container = QWidget()
        self.container.setLayout(self.layout)
        self.setCentralWidget(self.container)

        self.setWindowTitle('PyQt5 GUI Demo')
        self.setGeometry(300, 300, 300, 200)

    def on_click(self):
        text = self.textbox.text()

        if self.red_radio.isChecked():
            color = QColor(255, 0, 0)
        elif self.green_radio.isChecked():
            color = QColor(0, 255, 0)
        elif self.blue_radio.isChecked():
            color = QColor(0, 0, 255)
        else:
            color = QColor(0, 0, 0)

        self.result_label.setText(text)
        self.result_label.setStyleSheet(f"color: rgb({color.red()},
            {color.green()}, {color.blue()});")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())


致读者: 小时百科一直以来坚持所有内容免费,这导致我们处于严重的亏损状态。 长此以往很可能会最终导致我们不得不选择大量广告以及内容付费等。 因此,我们请求广大读者热心打赏 ,使网站得以健康发展。 如果看到这条信息的每位读者能慷慨打赏 10 元,我们一个星期内就能脱离亏损, 并保证在接下来的一整年里向所有读者继续免费提供优质内容。 但遗憾的是只有不到 1% 的读者愿意捐款, 他们的付出帮助了 99% 的读者免费获取知识, 我们在此表示感谢。

                     

友情链接: 超理论坛 | ©小时科技 保留一切权利