from PyQt5.Qt import *
import sys
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("社会我顺哥,人狠话不多")
window.resize(500, 500)
window.move(400, 200)
label = QLabel(window)
label.setText("Hello Sz")
label.move(200, 200)
window.show()
sys.exit(app.exec_())
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("事件消息的学习")
self.resize(500, 500)
self.setup_ui()
def setup_ui(self):
pass
def showEvent(self, QShowEvent):
print("窗口被展示了出来")
def closeEvent(self, QCloseEvent):
print("窗口被关闭了")
def moveEvent(self, QMoveEvent):
print("窗口被移动了")
def resizeEvent(self, QResizeEvent):
print("窗口改变了尺寸大小")
def enterEvent(self, QEvent):
print("鼠标进来了")
self.setStyleSheet("background-color: yellow;")
def leaveEvent(self, QEvent):
print("鼠标移开了")
self.setStyleSheet("background-color: green;")
def mousePressEvent(self, QMouseEvent):
print("鼠标被按下")
def mouseReleaseEvent(self, QMouseEvent):
print("鼠标被释放")
def mouseDoubleClickEvent(self, QMouseEvent):
print("鼠标双击")
def mouseMoveEvent(self, QMouseEvent):
print("鼠标移动了")
def keyPressEvent(self, QKeyEvent):
print("键盘上某一个按键被按下了")
def keyReleaseEvent(self, QKeyEvent):
print("键盘上某一个按键被释放了")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
def initUI(self):
btn1 = QPushButton('btn1', self)
btn1.move(50, 30)
btn2 = QPushButton('btn2', self)
btn2.move(50, 50)
btn1.clicked.connect(self.buttonClicked)
btn2.clicked.connect(self.buttonClicked)
self.setGeometry(300, 300, 300, 300)
self.setWindowTitle('Event Senden')
self.show()
def buttonClicked(self):
sender = self.sender()
self.statusBar().showMessage(sender.text() + " was clicked")
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("交互状态案例的学习")
self.resize(500, 500)
self.setup_ui()
def setup_ui(self):
# 添加三个子控件
label = QLabel(self)
label.setText("标签")
label.move(100, 50)
label.hide()
le = QLineEdit(self)
# le.setText("文本框")
le.move(100, 100)
btn = QPushButton(self)
btn.setText("登录")
btn.move(100, 150)
btn.setEnabled(False)
def text_cao(text):
print("文本内容发生了改变", text)
# if len(text) > 0:
# btn.setEnabled(True)
# else:
# btn.setEnabled(False)
btn.setEnabled(len(text) > 0)
le.textChanged.connect(text_cao)
def check():
# print("按钮被点击了")
# 1. 获取文本框内容
content = le.text()
# 2. 判定是否是Sz
if content == "1":
# 3. 是 -> 显示之前隐藏的提示标签, 展示文本
label.setText("登录成功")
else:
label.setText("登录失败")
label.show()
label.adjustSize()
btn.pressed.connect(check)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
qt54
#coding = utf-8
'''
这是一个猜数字的小游戏,在窗口中输入一个数字,点击我猜,看看你能猜中不!
文章链接:http://www.xdbcb8.com/archives/164.html
'''
import sys
from random import randint
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox, QLineEdit
from PyQt5.QtGui import QIcon
class Example(QWidget):
'''
猜数字的小游戏
'''
def __init__(self):
'''
一些初始设置
'''
super().__init__()
self.initUI()
self.num = randint(1, 100)
#我们随机生成一个数字,范围在1-100之间。
def initUI(self):
'''
绘制主要窗口,其中按钮“我猜”设置的提示是加粗的。
'''
self.setGeometry(300, 300, 300, 220)
self.setWindowTitle('学点编程吧--猜数字')
# self.setStyleSheet("""background:red;""")
self.setWindowIcon(QIcon('xdbcb8.ico'))
self.bt1 = QPushButton('我猜', self)
self.bt1.setGeometry(115, 150, 70 ,30)
self.bt1.setToolTip('点击这里猜数字')
#调用setTooltip()方法,可以使用富文本格式。
self.bt1.clicked.connect(self.showMessage)
self.text = QLineEdit('在这里输入数字', self)
self.text.selectAll()
self.text.setFocus()
#selectAll()方法则是可以理解为将“在这里输入数字”进行全选,方便输入数字,否则你还得手动全选删除默认字符。
#setFocus()就是让焦点置于文本栏中,方便用户输入。
self.text.setGeometry(80, 50, 150, 30)
self.text.setStyleSheet("""background-color:red;""")
# 输入栏的大小位置信息
self.show()
def showMessage(self):
'''
提示猜数字结果
'''
guessnumber = int(self.text.text())
if guessnumber > self.num:
QMessageBox.about(self, '看结果', '猜大了!')
self.text.setFocus()
# 焦点集中到输入框
elif guessnumber < self.num:
QMessageBox.about(self, '看结果', '猜小了!')
self.text.setFocus()
else:
QMessageBox.about(self, '看结果', '答对了!进入下一轮!')
self.num = randint(1, 100)
self.text.clear()
self.text.setFocus()
def closeEvent(self, event):
'''
如果关闭QWidget,则生成QCloseEvent。 要修改widget的行为,我们需要重新实现closeEvent()事件处理程序。
'''
reply = QMessageBox.question(self, '确认', '确认退出吗', QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
from PyQt5.QtWidgets import *
import sys
class myWinwows(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Hello World!")
self.setGeometry(200,100,500,500)
self.initUI()
def initUI(self):
layout=QHBoxLayout()
self.btn1=QPushButton('btn1',self)
self.btn1.move(10,20)
self.btn1.setStyleSheet("background-color:yellow;")
# btn1.setText("haha")
label=QLabel("helloworld",self)
label.move(250,100)
# label.setStyleSheet("border:black solid;border-radius:1px;")
label.setStyleSheet(" border:1px solid red;background-color:black;color:white;font-size:30px;border-radius:10px;")
label.adjustSize()
self.btn2=QPushButton('btn2',self)
self.btn2.move(100,20)
self.btn2.setStyleSheet("background-color:red;")
layout.addWidget(self.btn1)
layout.addWidget(self.btn2)
# layout.addWidget(label)
self.setLayout(layout)
self.btn1.clicked.connect(self.x)
self.btn2.clicked.connect(self.x2)
def x(self):
self.btn2.setStyleSheet("background-color:yellow;font-size:50px;")
def x2(self):
self.btn1.setStyleSheet("background-color:red;font-size:100px;")
if __name__ == '__main__':
app=QApplication(sys.argv)
my=myWinwows()
my.show()
sys.exit(app.exec_())
from PyQt5.QtWidgets import *
from PyQt5.Qt import *
import sys
import random
class myWinwows(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Hello World!")
self.setGeometry(200,100,500,500)
self.initUI()
def initUI(self):
self.label=QLabel("label")
self.fulllayout=QVBoxLayout()
self.btn11=QPushButton('btn1',self)
self.btn11.setStyleSheet('background:DarkRed;')
self.btn12=QPushButton('btn2',self)
self.btn12.setStyleSheet('background:Pink;')
self.btn13=QPushButton('btn3',self)
self.btn13.setStyleSheet('background:Crimson;')
self.btn14=QPushButton('btn4',self)
self.btn14.setStyleSheet('background:DarkSlateBlue;')
self.btn15=QPushButton('btn5',self)
self.btn15.setStyleSheet('background:Lime;')
self.btn16=QPushButton('btn6',self)
self.btn16.setStyleSheet('background:Gold;')
self.fulllayout.addWidget(self.btn11)
self.fulllayout.addStretch(1)
self.fulllayout1=QBoxLayout(QBoxLayout.LeftToRight)
self.fulllayout1.addWidget(self.btn12)
self.fulllayout1.addWidget(self.btn13)
self.fulllayout1.addWidget(self.btn14)
self.fulllayout.addStretch(2)
# self.fulllayout.insertStretch(3,3)
self.fulllayout.addLayout(self.fulllayout1)
self.fulllayout.addWidget(self.btn15)
self.fulllayout.addStretch(3)
self.fulllayout.addWidget(self.btn16)
self.fulllayout.addWidget(self.label)
self.label.setStyleSheet("""border:1px solid black;""")
# self.label.resize(200,200)
self.setLayout(self.fulllayout)
self.btn11.clicked.connect(self.label_action1)
self.btn12.clicked.connect(self.label_action12)
self.btn13.clicked.connect(self.label_action13)
self.btn14.clicked.connect(self.label_action14)
self.btn15.clicked.connect(self.label_action15)
# while True:
self.btn16.clicked.connect(self.label_action)
def label_action1(self):
self.label.setStyleSheet('background:DarkRed;')
def label_action12(self):
self.label.setStyleSheet('background:Pink;')
def label_action13(self):
self.label.setStyleSheet('background:Crimson;')
def label_action14(self):
self.label.setStyleSheet('background:DarkSlateBlue;')
def label_action15(self):
self.label.setStyleSheet('background:Lime;')
def label_action(self):
str=['LightPink','Pink','Crimson','DarkSlateBlue','Lime']
random_ = random.randint(0, 5)
self.label.setStyleSheet('''background:{};'''.format(str[random_]))
if __name__ == '__main__':
app=QApplication(sys.argv)
my=myWinwows()
my.show()
sys.exit(app.exec_())
def initUI(self):
btn1 = QPushButton("btn1")
btn2 = QPushButton("btn2")
btn3 = QPushButton("btn3")
vlayout = QVBoxLayout(self)
vlayout.addWidget(btn1)
vlayout.addStretch(1)
vlayout.addWidget(btn2)
vlayout.addStretch(2)
vlayout.addWidget(btn3)
http://c.biancheng.net/view/1845.html
def initUI(self):
spinbox=QSpinBox(self)
double_spinbox=QDoubleSpinBox(self)
spinbox.move(200,200)
double_spinbox.move(400,200)
spinbox.setPrefix('$')
spinbox.setSingleStep(2)
# spinbox.setMinimum(10)
spinbox.setMaximum(500)
spinbox.setValue(20)
spinbox.setSuffix('KG')
spinbox.adjustSize()
print(spinbox.singleStep())
print(spinbox.minimum())
print(spinbox.value())
# help(QSpinBox.setSingleStep)
spin box 数字调整框;旋转框;微调方块
def initUI(self):
self.btn=QPushButton('btn',self)
self.btn.setText('hello')
self.le=QLineEdit('lineedit',self)
self.le.move(200,400)
self.spinbox=QSpinBox(self)
double_spinbox=QDoubleSpinBox(self)
self.spinbox.move(200,200)
double_spinbox.move(400,200)
self.spinbox.setPrefix('$')
self.spinbox.setSingleStep(2)
# spinbox.setMinimum(10)
self.spinbox.setMaximum(500)
self.spinbox.setValue(20)
self.spinbox.setSuffix('KG')
self.spinbox.adjustSize()
# self.spinbox.setStyleSheet('background-color:red;')
print(self.spinbox.singleStep())
print(self.spinbox.minimum())
print(self.spinbox.value())
self.btn.clicked.connect(self.jud)
# self.btn.clicked.connect(self.jud())
# 注意以上
def jud(self):
# self.spinbox.setStyleSheet('background-color;red')
x=(self.le.text())
if (x)=='5':
print("you are so good!")
QSlider:滑动条,通过滑动来设置数值,可用于数值输入。实例中使用 4 个滑动条输入红、绿、蓝三色和 Alpha 值,然后合成颜色,作为一个 QTextEdit 组件的底色。
QScrollBar:卷滚条,与 QSlider 功能类似,还可以用于卷滚区域。
QProgressBar:进度条,一般用于显示任务进度,可用于数值的百分比显示。实例程序中滑动一个Slider,获取其值并更新ScrollBar和ProgressBar。
QDial:表盘式数值输入组件,通过转动表针获得输入值。
QLCDNumber:模仿 LCD 数字的显示组件,可以显示整数或浮点数,显示整数时可以不同进制显示。实例程序中转动表盘,获得的值显示在 LCD 组件中。单击“LCD显示进制”的 RadioButton 时,设置 LCD 的显示进制。
def initUI(self):
layout=QBoxLayout(QBoxLayout.TopToBottom)
slider=QSlider(self)
slider.setTickPosition(5)
help(slider.setTickPosition)
label=QLabel()
# if 'setText' in dir(label):
# print(1)
# else:print(0)
scrollbar=QScrollBar(self)
pregressbar=QProgressBar()
dial=QDial()
lcdnumber=QLCDNumber()
#layout
layout.addWidget(slider)
layout.addWidget(scrollbar)
layout.addWidget(pregressbar)
layout.addWidget(dial)
layout.addWidget(lcdnumber)
self.setLayout(layout)
#!usr/bin/python
# -*- coding: utf-8 -*-
# 1. QAbstractButton button子类:QToolButton,QCheckBox,QRadioButton,QPushButton,QCommandLinkButton
# 抽象类需要继承并重写paintEvent方法
from PyQt5.Qt import *
import sys
app = QApplication(sys.argv)
# 自定义button
class Btn(QAbstractButton):
def paintEvent(self, e: QPaintEvent) -> None:
painter = QPainter(self)
pen = QPen(QColor(100, 100, 50), 5)
painter.setPen(pen)
painter.drawText(25, 25, self.text())
painter.drawEllipse(0, 0, 100, 100)
# 2.指定用户点击的有效区域,返回值True响应点击,False不响应
def hitButton(self, pos: QPoint) -> bool:
# 仅点击右半边才响应
if pos.x() > self.width() / 2:
return True
return False
window = QWidget()
window.resize(500, 500)
btn = Btn(window)
# 3. 设置文本
btn.setText("自定义按钮")
print(btn.text())
# 4. 通过设置快捷键点击按钮
btn.setShortcut("Ctrl+a")
# 5.设置图标
btn.setIcon(QIcon("icon.png"))
btn.setIconSize(QSize(20, 20))
print(btn.icon())
print(btn.iconSize())
btn.move(100, 100)
btn.clicked.connect(lambda: print("btn被点击了"))
btn.pressed.connect(lambda: print("btn被按下了"))
btn.released.connect(lambda: print("鼠标被释放了"))
btn.toggled.connect(lambda: print("选中状态被切换了"))
# 6.自动重复指的是,如果用户按住某个键不松开的话,就不断的自动重复它,不断触发它所对应的槽函数
# 设置为true后一直触发clicked的槽函数
# 设置自动重复延迟和时间间隔
btn.setAutoRepeatDelay(1000)
btn.setAutoRepeatInterval(100)
btn.setAutoRepeat(True)
# 上述方法去掉set就是获取值
print(btn.autoRepeatDelay())
# 7.设置按钮是否被按下,按键按下去,不松手
# btn.setDown(True)
# btn.setStyleSheet("Btn:pressed {background-color:red;}")
print(btn.isDown())
checkbox = QCheckBox(window)
checkbox.move(50, 50)
checkbox.setText("男")
# 8.判断checkbox是否被选中,设置选中与否,切换选中状态
print(checkbox.isChecked())
checkbox.setChecked(True)
checkbox.toggle()
checkbox1 = QCheckBox(window)
checkbox1.move(100, 50)
checkbox1.setText("女")
# 9.排他性多个按钮都设置了排他性,那么就只能有一个被选中:就是类似于男女性别的设置
checkbox1.setAutoExclusive(True)
# 10.用代码模拟用户的点击
checkbox1.click()
# 带动画效果的点击
checkbox1.animateClick(1000)
window.show()
sys.exit(app.exec_())```