栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Windows+Python使用PyQt5----简单样例(二)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Windows+Python使用PyQt5----简单样例(二)

Windows+Python使用PyQt5----简单样例(二)
  • 使用PyQT图片加载
  • 使用PyQT下拉选择
  • 使用PyQT选择框
  • 使用PyQT单选框
  • 使用PyQT单选框

使用PyQT图片加载
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QGridLayout, QWidget
from PyQt5.QtGui import QPixmap

class Example(QWidget):

    def __init__(self):
        super().__init__()

        # 加载图片到内存
        self.im = QPixmap("./test.png")
        # 因为图片的依靠标签来进行展示
        self.label = QLabel()
        # 将标签设置为pixmap
        self.label.setPixmap(self.im)

        self.label1 = QLabel()
        # 将标签设置为pixmap
        self.label1.setPixmap(self.im)

        # 采用格子布局,并将图片放在格子中
        self.grid = QGridLayout()
        self.grid.addWidget(self.label,1,1)
        self.grid.addWidget(self.label1,2,2)
        self.setLayout(self.grid)

        self.setGeometry(250,250,320,200)
        self.setWindowTitle("PyQT show image")
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

使用PyQT下拉选择
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QComboBox, QPushButton

class Example(QMainWindow):
    
    def __init__(self):
        super().__init__()
                
        # 设置下拉选项组件
        combo = QComboBox(self)
        combo.addItem("Apple")
        combo.addItem("Pear")
        combo.addItem("Lemon")

        combo.move(50, 50)

        # 测试按钮的参数传递
        button = QPushButton(self)
        button.setText("TTT")
        button.clicked.connect(lambda: self.showDialog("arg"))

        self.qlabel = QLabel(self)
        self.qlabel.move(50,16)

        # 把组件上的值传给响应函数
        combo.activated[str].connect(self.onChanged)      

        self.setGeometry(250,250,320,200)
        self.setWindowTitle("QLineEdit Example")
        self.show()

    def onChanged(self, text):
        self.qlabel.setText(text)
        self.qlabel.adjustSize()

    def showDialog(self, text):
        self.qlabel.setText(text)
        self.qlabel.adjustSize() 
        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


使用PyQT选择框
from PyQt5.QtWidgets import *
import sys

class Window(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        # 初始化格子布局
        layout = QGridLayout()
        self.setLayout(layout)

        # 实例化单选框
        cbutton = QCheckBox("I have a Cat")
        # 设置默认值
        cbutton.setChecked(True)
        # 设置属性,其中的animal可以自定义
        cbutton.animal = "Cat"
        # 设置响应函数
        cbutton.toggled.connect(self.onClicked)
        layout.addWidget(cbutton, 1, 1)

    def onClicked(self):
        # 获取激活该响应函数的控件
        cbutton = self.sender()
        # 获取控件当前的状态
        print("Animal " + (cbutton.animal) + " is " + str(cbutton.isChecked()))

app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())


使用PyQT单选框
from PyQt5.QtWidgets import *
import sys

class Window(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        layout = QGridLayout()
        self.setLayout(layout)

        radiobutton = QRadioButton("Australia")
        radiobutton.setChecked(True)
        radiobutton.country = "Australia"
        radiobutton.toggled.connect(self.onClicked)
        layout.addWidget(radiobutton, 0, 0)

        radiobutton = QRadioButton("China")
        radiobutton.country = "China"
        radiobutton.toggled.connect(self.onClicked)
        layout.addWidget(radiobutton, 0, 1)

        radiobutton = QRadioButton("Japan")
        radiobutton.country = "Japan"
        radiobutton.toggled.connect(self.onClicked)
        layout.addWidget(radiobutton, 0, 2)

    def onClicked(self):
        radioButton = self.sender()
        if radioButton.isChecked():
            print("Country is %s" % (radioButton.country))

app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())


使用PyQT单选框
from PyQt5.QtWidgets import *
import sys

class GroupBox(QWidget):

    def __init__(self):
        QWidget.__init__(self)

        self.setWindowTitle("GroupBox")
        layout = QGridLayout()
        self.setLayout(layout)

        # 初始化成组的单选
        # 如果顶层是True,则下层可以选择
        # 如果顶层是False,则下层不可选择
        groupbox = QGroupBox("GroupBox Example")
        groupbox.setCheckable(True)
        layout.addWidget(groupbox)
        
        # 构件一个VBox布局
        vbox = QVBoxLayout()
        groupbox.setLayout(vbox)

        radiobutton = QRadioButton("RadioButton 1")
        vbox.addWidget(radiobutton)
        
        radiobutton = QRadioButton("RadioButton 2")
        vbox.addWidget(radiobutton)

        radiobutton = QRadioButton("RadioButton 3")
        vbox.addWidget(radiobutton)

        radiobutton = QRadioButton("RadioButton 4")
        vbox.addWidget(radiobutton)
        
        
app = QApplication(sys.argv)
screen = GroupBox()
screen.show()
sys.exit(app.exec_())


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1009030.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号