PyQt5 – 改变Push按钮的文字字体和大小
在这篇文章中,我们将看到如何改变Push Button的文字风格或大小。
QPushButton 是PyQt中的一个简单的按钮,当用户点击时,一些相关的动作会被执行。为了在应用程序中添加这个按钮,我们使用了QPushButton类。
为了设置字体,我们将使用setFont方法,该方法以QFont对象作为参数。
语法: button.setFont(QFont(‘Arial’, 15))
参数: 它需要两个参数,第一个是字体名称,另一个是整数,指的是文本的大小。
执行的动作 它改变了文本的大小和字体。
代码:
# importing libraries
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
# setting title
self.setWindowTitle("Python ")
# setting geometry
self.setGeometry(100, 100, 600, 400)
# calling method
self.UiComponents()
# showing all the widgets
self.show()
# method for widgets
def UiComponents(self):
# creating a push button
button = QPushButton("CLICK", self)
# setting geometry of button
button.setGeometry(200, 150, 100, 40)
# changing font and size of text
button.setFont(QFont('Times', 15))
# adding action to a button
button.clicked.connect(self.clickme)
# action method
def clickme(self):
# printing pressed
print("pressed")
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :