PyQt5学习笔记2

5 篇文章 0 订阅
1 篇文章 0 订阅

gridLayout

#coding = 'utf-8'

'''
这是一个格栅布局的小例子!
文章链接:http://www.xdbcb8.com/archives/209.html
'''

import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, QApplication, QGridLayout, QLCDNumber)

class Example(QWidget):
    '''
    格栅布局
    '''
    def __init__(self):
        '''
        一些初始设置
        '''
        super().__init__()
        self.Init_UI()

    def Init_UI(self):
        '''
        界面初始设置
        '''
        grid = QGridLayout()
        self.setLayout(grid)

        self.setGeometry(300, 300, 400, 300)
        self.setWindowTitle('学点编程吧-计算器')
        
        self.lcd = QLCDNumber()
        
        grid.addWidget(self.lcd, 0, 0, 3, 0)#我们使QLCDNumber小部件跨越4行
        grid.setSpacing(10)#将垂直和水平间距设置为10
        
        names = ['Cls', 'Bc', '', 'Close',
                 '7', '8', '9', '/',
                '4', '5', '6', '*',
                 '1', '2', '3', '-',
                '0', '.', '=', '+']
                
        positions = [(i, j) for i in range(4, 9) for j in range(4, 8)]#将小部件添加到窗口中

        for position, name in zip(positions, names):
            #小部件的上的名称和它们的位置一一对应起来,注意zip的用法
            if name == '':
                continue
            button = QPushButton(name)
            grid.addWidget(button, *position)
            button.clicked.connect(self.Cli)
        
        self.show()
    
    def Cli(self):
        '''
        点击按钮时对应的槽函数
        '''
        sender = self.sender().text()
        ls = ['/', '*', '-', '=', '+']
        if sender in ls:
            self.lcd.display('A')#当我们点击'/', '*', '-', '=', '+'时,LCD上显示'A'
        else:
            self.lcd.display(sender)#反之显示按钮上的名称,如:1

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

radiobutton实践,buttongroup

from PyQt5 import *
from PyQt5.Qt import *
import sys
class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("HelloWorld")
        self.resize(500, 500)
        self.initUI()
    def initUI(self):
        layout=QBoxLayout(QBoxLayout.TopToBottom)

        layout_l=QBoxLayout(QBoxLayout.TopToBottom)
        self.rb11=QRadioButton('Yur are',self)
        self.rb12=QRadioButton('I am',self)
        self.rb13=QRadioButton('He is',self)
        layout_l.addWidget(self.rb11)
        layout_l.addWidget(self.rb12)
        layout_l.addWidget(self.rb13)
        layout_r=QBoxLayout(QBoxLayout.TopToBottom)
        self.rb21=QRadioButton('大美女',self)
        self.rb22=QRadioButton('大帅哥',self)
        self.rb23=QRadioButton('小屁孩',self)
        layout_r.addWidget(self.rb21)
        layout_r.addWidget(self.rb22)
        layout_r.addWidget(self.rb23)

        layout_lr=QBoxLayout(QBoxLayout.LeftToRight)
        layout_lr.addLayout(layout_l)
        layout_lr.addLayout(layout_r)


        self.bg1=QButtonGroup(self)
        self.bg1.addButton(self.rb11,11)
        self.bg1.addButton(self.rb12,12)
        self.bg1.addButton(self.rb13,13)

        self.bg2=QButtonGroup(self)
        self.bg2.addButton(self.rb21,21)
        self.bg2.addButton(self.rb22,22)
        self.bg2.addButton(self.rb23,23)
        submit_button=QPushButton('Submit',self)
        layout.addLayout(layout_lr)
        layout.addWidget(submit_button)
        self.setLayout(layout)
        self.info1=''
        self.info2=''
        submit_button.clicked.connect(self.submit)
        self.bg1.buttonClicked.connect(self.rbclicked)
        self.bg2.buttonClicked.connect(self.rbclicked)
    def submit(self):
        if self.info1=='' or self.info2=='':
            QMessageBox.information(self,'What?','bro,你没有选,请速速选择!')
        else:
            QMessageBox.information(self,'What?',self.info1+self.info2)
    def rbclicked(self):
        sender=self.sender()
        if sender==self.bg1:
            if self.bg1.checkedId() == 11:
                self.info1 = '你是'
            elif self.bg1.checkedId() == 12:
                self.info1 = '我是'
            elif self.bg1.checkedId() == 13:
                self.info1 = '他是'
            else:
                self.info1 = ''
        else:
            if self.bg2.checkedId() == 21:
                self.info2 = '大美女'
            elif self.bg2.checkedId() == 22:
                self.info2 = '大帅哥'
            elif self.bg2.checkedId() == 23:
                self.info2 = '小屁孩'
            else:
                self.info2 = ''

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

QMessageBox

    def initUI(self):
        layout=QBoxLayout(QBoxLayout.TopToBottom)
        self.label=QLabel('你选择了',self)
        # layout.addStretch(1)
        self.button1=QPushButton('提醒')
        self.button12=QPushButton('询问')
        self.button13=QPushButton('警告')
        layout_h1=QBoxLayout(QBoxLayout.LeftToRight)
        layout_h1.addWidget(self.button1)
        layout_h1.addWidget(self.button12)
        layout_h1.addWidget(self.button13)
        layout_h2=QBoxLayout(QBoxLayout.LeftToRight)
        self.button14=QPushButton('错误')
        self.button15=QPushButton('关于')
        self.button16=QPushButton('关于Qt')
        layout_h2.addWidget(self.button14)
        layout_h2.addWidget(self.button15)
        layout_h2.addWidget(self.button16)
        layout.addStretch(1)
        layout.addWidget(self.label,1)
        layout.addLayout(layout_h1)
        layout.addStretch(1)
        layout.addLayout(layout_h2)
        layout.addStretch(2)

        self.setLayout(layout)
        self.button1.clicked.connect(self.info)
        self.button12.clicked.connect(self.question)

        self.button15.clicked.connect(self.about)

    def info(self):

        reply=QMessageBox.information(self,'提醒','这是一个对话框',QMessageBox.Ok | QMessageBox.Close,QMessageBox.Close)
        if reply==QMessageBox.Ok:
            self.label.setText('你选择了Ok!')
        else:
            self.label.setText('你选择了Close!')
    def question(self):
        reply=QMessageBox.information(self,'询问','这是一个询问消息对话框',QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel |QMessageBox.Ok,QMessageBox.Cancel)
        if reply == QMessageBox.Yes:
            self.label.setText('你选择了Yes!')
        elif reply == QMessageBox.No:
            self.label.setText('你选择了No!')
        elif reply==QMessageBox.Cancel:
            self.label.setText('你选择了Cancel!')
        else:
            self.label.setText('fuck you !')
    def about(self):
        # msgbox=QMessageBox(QMessageBox.NoIcon,'ABOUT','不要意淫了,早点睡觉')
        msgbox=QMessageBox.about(self,'ABOUT','不要意淫了,早点睡觉')
        # msgbox.exec()

boxLayout direction

label1 = QLabel("标签1",self)
        label1.setStyleSheet("background-color:red;")
        label2 = QLabel("标签2",self)
        label2.setStyleSheet("background-color:green;")
        label3 = QLabel("标签3",self)
        label3.setStyleSheet("background-color:yellow;")

        # #
        boxLayout = QBoxLayout(QBoxLayout.BottomToTop)
        #
        boxLayout.addWidget(label1)
        boxLayout.addWidget(label2)
        boxLayout.addWidget(label3)

        # boxLayout.setDirection(3)
        help(boxLayout.direction())
        print()
        timer = QTimer(self)

        def timeout_slot():
            boxLayout.setDirection((boxLayout.direction() + 1) % 4)
            pass

        timer.timeout.connect(timeout_slot)
        timer.start(1000)

        self.setLayout(boxLayout)

formlayout

    def initUI(self):
        label1=QLabel('name(&n)',self)
        label2=QLabel('age')
        label3=QLabel('sex')

        name_le=QLineEdit(self)
        age_spinbox=QSpinBox(self)
        age_spinbox.setSuffix('岁')
        formlayout=QFormLayout()


        formlayout.addRow(label1,name_le)
        formlayout.addRow(label2,age_spinbox)

        male_radioButton=QPushButton('male')
        male_radioButton.setStyleSheet('background-color:red;')
        female_radioButton=QPushButton('female')
        female_radioButton.setStyleSheet('background-color:green;')

        sex_h_layout=QHBoxLayout()
        sex_h_layout.addWidget(male_radioButton)
        sex_h_layout.addWidget(female_radioButton)

        formlayout.addRow(label3,sex_h_layout)

        # print(formlayout.getWidgetPosition(label1))
        # print(formlayout.getWidgetPosition(label2))
        # print(formlayout.getLayoutPosition(sex_h_layout))
        # print(formlayout.rowCount())

        # print(QFormLayout.LabelRole)
        # print(QFormLayout.FieldRole)
        # print(QFormLayout.SpanningRole)

        formlayout.labelForField(name_le).setText('haha')
        # mylabel.setText("姓名(Name)")

        submit=QPushButton('Submit')
        formlayout.addRow(submit)
        self.setLayout(formlayout)
        formlayout.setFormAlignment(Qt.AlignLeft)
        formlayout.setLabelAlignment(Qt.AlignCenter)
        # formlayout.setHorizontalSpacing(100)
        # formlayout.setVerticalSpacing(100)
        formlayout.setSpacing(50)

stackLayot

    def initUI(self):

        stacklayout=QStackedLayout()
        self.setLayout(stacklayout)
        label1 = QLabel("标签1")
        label1.setStyleSheet("background-color:red;")
        label2 = QLabel("标签2")
        label2.setStyleSheet("background-color:green;")
        label3 = QLabel("标签3")
        label3.setStyleSheet("background-color:yellow;")
        label4 = QLabel("标签4")
        label4.setStyleSheet("background-color:cyan;")
        label5 = QLabel("标签5")
        label5.setStyleSheet("background-color:blue;")

        v_layout = QVBoxLayout()
        v_layout.addWidget(label4)
        v_layout.addWidget(label5)

        stacklayout.addWidget(label1)
        stacklayout.addWidget(label2)
        stacklayout.addWidget(label3)

        timer = QTimer(self)

        def timeout_slot():
            stacklayout.setCurrentIndex((stacklayout.currentIndex() + 1) % stacklayout.count())
            # print(stacklayout.currentIndex())
            # print(stacklayout.count())
        timer.timeout.connect(timeout_slot)
        timer.start(1000)

textEdit

    def setupUI(self):
        btn1=QPushButton('clear',self)
        textEdit=QTextEdit(self)
        textEdit.setText('<b>hello World!</b>')
        textEdit.move(self.width()//4,self.height()//4)
        textEdit.setStyleSheet('background-color:cyan;border:1px solid black')
        textEdit.setPlaceholderText('fuck you')
        textEdit.append('xxxxx')
        textEdit.append('x')
        self.textedit=textEdit
        btn1.pressed.connect(self.textedit_clear)
        btn1.move(self.width()//5,self.height()//5)

        self.btn1=btn1
        # print(self.textedit.styleSheet())
    def textedit_clear(self):
        x=self.btn1.styleSheet()
        print(x)
        x1='background-color:red;'
        x2='background-color:green;'
        self.textedit.clear()
        if x==x1:self.btn1.setStyleSheet(x2)
        else:self.btn1.setStyleSheet(x1)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值