"""
構(gòu)造函數(shù):
QPushButton() :創(chuàng)建一個(gè)沒有文本和父控件的按鈕。
QPushButton(parent) :創(chuàng)建一個(gè)沒有文本但有指定父控件的按鈕。
QPushButton(text) :創(chuàng)建一個(gè)帶有指定文本且沒有父控件的按鈕。
QPushButton(text, parent) :創(chuàng)建一個(gè)帶有指定文本和父控件的按鈕。
屬性設(shè)置方法:
setText(text) :設(shè)置按鈕上顯示的文本。
setIcon(icon) :為按鈕設(shè)置圖標(biāo)。
setIconSize():設(shè)置圖片大小
setEnabled(bool) :設(shè)置按鈕是否可用。
setFlat(bool) :設(shè)置按鈕是否為扁平樣式。
setCheckable(bool) :設(shè)置按鈕是否為可復(fù)選的。
setChecked(bool) :設(shè)置復(fù)選按鈕的選中狀態(tài)。
setAutoExclusive() 設(shè)置排他性
#自定義按鈕
信號(hào):
clicked(bool) :當(dāng)按鈕被點(diǎn)擊時(shí)發(fā)出,bool 參數(shù)表示按鈕是否為切換按鈕以及其新的狀態(tài)(僅在按鈕是可切換時(shí)有用)。
pressed() :當(dāng)鼠標(biāo)按下按鈕時(shí)發(fā)出。
released() :當(dāng)鼠標(biāo)在按鈕上釋放時(shí)發(fā)出。
toggled(bool) :當(dāng)可切換按鈕的狀態(tài)改變時(shí)發(fā)出。
"""
from PySide6.QtWidgets import *
from PySide6.QtCore import *
from PySide6.QtGui import *
class Btn(QPushButton):
def mousePressEvent(self, e):
if e.button()== Qt.MouseButton.LeftButton:
self.setStyleSheet("background-color:rgb(0,234,0);"
f"border-radius:{self.width()/2}px;"
"border:3px groove gray;"
"border-style:outset")
super().mousePressEvent(e)
def mouseReleaseEvent(self, e):
if e.button() == Qt.MouseButton.LeftButton:
self.setStyleSheet("background-color:rgb(58,111,0);"
f"border-radius:{self.width() / 2}px;"
"border:3px groove gray;"
"border-style:outset")
super().mouseReleaseEvent(e)
def resizeEvent(self, event):
self.setStyleSheet("background-color:rgb(58,111,0);"
f"border-radius:{self.width() / 2}px;"
"border:3px groove gray;"
"border-style:outset")
super().resizeEvent(event)
class Window(QWidget):
def __init__(self):
super().__init__()
self.resize(500,500)
self.setWindowTitle("QPushButton學(xué)習(xí)")
#創(chuàng)建一個(gè)按鈕方式一
self.btn = QPushButton()
self.btn.setText("button1")
self.btn.setParent(self)
# self.btn.show()
#創(chuàng)建一個(gè)按鈕方式二
self.btn2 = QPushButton('button2',self)
self.btn2.move(100,100)
self.btn2.resize(80,80)
self.btn3 = Btn("button3",self)
self.btn3.setGeometry(300,50,80,80)
#給按鈕設(shè)置圖片,并設(shè)定大小
self.btn2.setIcon(QIcon("view.jpg"))
self.btn2.setIconSize(QSize(50, 50))
#設(shè)定狀態(tài)
self.btn.setCheckable(True)
self.btn2.setCheckable(True)
# self.btn2.setChecked(True)
#設(shè)置是否可用
# self.btn2.setEnabled(False)
#設(shè)置排他性
self.btn.setAutoExclusive(True)
self.btn2.setAutoExclusive(True)
#信號(hào)
self.btn3.clicked.connect(lambda :print("按鈕被點(diǎn)擊了"))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec())
承擔(dān)因您的行為而導(dǎo)致的法律責(zé)任,
本站有權(quán)保留或刪除有爭(zhēng)議評(píng)論。
參與本評(píng)論即表明您已經(jīng)閱讀并接受
上述條款。