PyQt5 控件学习(一个一个学习之QFontDialog)
QFontDialog的继承图:
QFontDialog的描述:
QFontDialog的继承:
它是继承自它的父类 QDialog 的
QFontDialog的功能作用:
QFontDialog的功能作用之构造函数:
第一种构造方法:
from PyQt5.Qt import * #刚开始学习可以这样一下导入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QFontDialog的学习") self.resize(400,400) self.set_ui() def set_ui(self): fontDialog = QFontDialog(self) self.fontDialog = fontDialog self.test() # fontDialog.show() # fontDialog.open() # fontDialog.exec() def test(self): btn = QPushButton(self) btn.setText("按钮") btn.move(0,300) btn.clicked.connect(lambda :self.fontDialog.open()) if __name__ == '__main__': app =QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
第二种构造方法:
在构造的时候就传入个font 对象 ,
from PyQt5.Qt import * #刚开始学习可以这样一下导入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QFontDialog的学习") self.resize(400,400) self.set_ui() def set_ui(self): font = QFont() font.setFamily("宋体") font.setPointSize(14) fontDialog = QFontDialog(font,self) self.fontDialog = fontDialog self.test() # fontDialog.show() # fontDialog.open() # fontDialog.exec() def test(self): btn = QPushButton(self) btn.setText("按钮") btn.move(0,300) btn.clicked.connect(lambda :self.fontDialog.open()) if __name__ == '__main__': app =QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
QFontDialog的功能作用之打开对话框:
from PyQt5.Qt import * #刚开始学习可以这样一下导入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QFontDialog的学习") self.resize(400,400) self.set_ui() def set_ui(self): font = QFont() font.setFamily("宋体") font.setPointSize(14) fontDialog = QFontDialog(font,self) self.fontDialog = fontDialog self.test() # fontDialog.show() # fontDialog.open() # fontDialog.exec() def test(self): btn = QPushButton(self) btn.setText("按钮") btn.move(0,300) def font_open_slot(): print("字体已经被选择好了") btn.clicked.connect(lambda :self.fontDialog.open(font_open_slot)) if __name__ == '__main__': app =QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
也可以借助exec() 来获取用户点击的是哪个字体:
from PyQt5.Qt import * # 刚开始学习可以这样一下导入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QFontDialog的学习") self.resize(400, 400) self.set_ui() def set_ui(self): font = QFont() font.setFamily("宋体") font.setPointSize(14) fontDialog = QFontDialog(font, self) self.fontDialog = fontDialog self.test() # fontDialog.show() # fontDialog.open() # fontDialog.exec() def test(self): btn = QPushButton(self) btn.setText("按钮") btn.move(0, 300) def font_open_slot(): print("字体已经被选择好了", self.fontDialog.selectedFont().family()) # btn.clicked.connect(lambda :self.fontDialog.open(font_open_slot)) ########################################################### # 利用 exec() 返回值判断用户点击了哪个选项 print(self.fontDialog.exec()) #点关闭是0 点确认是 1 if self.fontDialog.exec(): print(self.fontDialog.selectedFont().family()) ########################################################### if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
QFontDialog的功能作用之当前字体:
from PyQt5.Qt import * # 刚开始学习可以这样一下导入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QFontDialog的学习") self.resize(400, 400) self.set_ui() def set_ui(self): font = QFont() font.setFamily("宋体") font.setPointSize(14) # fontDialog = QFontDialog(font, self) fontDialog = QFontDialog(self) self.fontDialog = fontDialog self.test() ############################设置当前字体############################### fontDialog.setCurrentFont(font) ############################设置当前字体############################### def test(self): btn = QPushButton(self) btn.setText("按钮") btn.move(0, 300) def font_open_slot(): print("字体已经被选择好了", self.fontDialog.selectedFont().family()) btn.clicked.connect(lambda :self.fontDialog.open(font_open_slot)) if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
QFontDialog的功能作用之最终选中字体:
from PyQt5.Qt import * #刚开始学习可以这样一下导入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QFontDialog的学习") self.resize(400,400) self.set_ui() def set_ui(self): font = QFont() font.setFamily("宋体") font.setPointSize(14) fontDialog = QFontDialog(font,self) self.fontDialog = fontDialog self.test() # fontDialog.show() # fontDialog.open() # fontDialog.exec() def test(self): btn = QPushButton(self) btn.setText("按钮") btn.move(0,300) def font_open_slot(): print("字体已经被选择好了",self.fontDialog.selectedFont()) btn.clicked.connect(lambda :self.fontDialog.open(font_open_slot)) if __name__ == '__main__': app =QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
from PyQt5.Qt import * #刚开始学习可以这样一下导入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QFontDialog的学习") self.resize(400,400) self.set_ui() def set_ui(self): font = QFont() font.setFamily("宋体") font.setPointSize(14) fontDialog = QFontDialog(font,self) self.fontDialog = fontDialog self.test() # fontDialog.show() # fontDialog.open() # fontDialog.exec() def test(self): btn = QPushButton(self) btn.setText("按钮") btn.move(0,300) def font_open_slot(): print("字体已经被选择好了",self.fontDialog.selectedFont().family()) btn.clicked.connect(lambda :self.fontDialog.open(font_open_slot)) if __name__ == '__main__': app =QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
QFontDialog的功能作用之选项控制:
from PyQt5.Qt import * # 刚开始学习可以这样一下导入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QFontDialog的学习") self.resize(400, 400) self.set_ui() def set_ui(self): font = QFont() font.setFamily("宋体") font.setPointSize(14) fontDialog = QFontDialog(font, self) self.fontDialog = fontDialog fontDialog.setOption(QFontDialog.NoButtons) #此时就没有下面的按钮了 self.test() fontDialog.show() return None def test(self): btn = QPushButton(self) btn.setText("按钮") btn.move(0, 300) return None def font_open_slot(): print("字体已经被选择好了", self.fontDialog.selectedFont().family()) btn.clicked.connect(lambda :self.fontDialog.open(font_open_slot)) if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
它的应用场景是:
这里要借助一个信号 currentFontChanged信号 ,
from PyQt5.Qt import * # 刚开始学习可以这样一下导入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QFontDialog的学习") self.resize(400, 400) self.set_ui() def set_ui(self): font = QFont() font.setFamily("宋体") font.setPointSize(14) fontDialog = QFontDialog(font, self) self.fontDialog = fontDialog fontDialog.setOption(QFontDialog.NoButtons) #此时就没有下面的按钮了 self.test() fontDialog.show() label = QLabel(self) label.move(200,200) label.setText("我爱中国") def fontDialog_currentFontChanged_slot(font): label.adjustSize() label.setFont(font) fontDialog.currentFontChanged.connect(fontDialog_currentFontChanged_slot) return None def test(self): return None btn = QPushButton(self) btn.setText("按钮") btn.move(0, 300) return None def font_open_slot(): print("字体已经被选择好了", self.fontDialog.selectedFont().family()) btn.clicked.connect(lambda :self.fontDialog.open(font_open_slot)) if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
也可以设置多个选项,
如下:显示等宽字体:
from PyQt5.Qt import * # 刚开始学习可以这样一下导入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QFontDialog的学习") self.resize(400, 400) self.set_ui() def set_ui(self): font = QFont() font.setFamily("宋体") font.setPointSize(14) fontDialog = QFontDialog(font, self) self.fontDialog = fontDialog # fontDialog.setOption(QFontDialog.NoButtons) #此时就没有下面的按钮了 fontDialog.setOptions(QFontDialog.NoButtons|QFontDialog.MonospacedFonts) #显示等宽字体 self.test() fontDialog.show() label = QLabel(self) label.move(200,200) label.setText("我爱中国") def fontDialog_currentFontChanged_slot(font): label.adjustSize() label.setFont(font) fontDialog.currentFontChanged.connect(fontDialog_currentFontChanged_slot) return None def test(self): return None btn = QPushButton(self) btn.setText("按钮") btn.move(0, 300) return None def font_open_slot(): print("字体已经被选择好了", self.fontDialog.selectedFont().family()) btn.clicked.connect(lambda :self.fontDialog.open(font_open_slot)) if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
QFontDialog的功能作用之静态方法:
from PyQt5.Qt import * # 刚开始学习可以这样一下导入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QFontDialog的学习") self.resize(400, 400) self.set_ui() def set_ui(self): font = QFont() font.setFamily("宋体") font.setPointSize(14) fontDialog = QFontDialog(font, self) self.fontDialog = fontDialog self.test() label = QLabel(self) self.label = label label.move(200,200) label.setText("我爱中国") def test(self): btn = QPushButton(self) btn.setText("按钮") btn.move(0, 300) def btn_clicked_slot(): ret = QFontDialog.getFont(self) # 它会自动的展示 字体对话框 #这里只是给他设置个父控件 返回值的true false 指的是是否选中了确认 # print(ret) if ret[1]: self.label.setFont(ret[0]) self.label.adjustSize() btn.clicked.connect(btn_clicked_slot) if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
更多参数的静态方法 getFont()
from PyQt5.Qt import * # 刚开始学习可以这样一下导入 import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QFontDialog的学习") self.resize(400, 400) self.set_ui() def set_ui(self): font = QFont() self.font = font font.setFamily("宋体") font.setPointSize(14) fontDialog = QFontDialog(font, self) self.fontDialog = fontDialog self.test() label = QLabel(self) self.label = label label.move(200,200) label.setText("我爱中国") def test(self): btn = QPushButton(self) btn.setText("按钮") btn.move(0, 300) def btn_clicked_slot(): # ret = QFontDialog.getFont(self) ret = QFontDialog.getFont(self.font,self,"选择一个好看的字体") #相对上一个更多一些选项 # print(ret) if ret[1]: self.label.setFont(ret[0]) self.label.adjustSize() btn.clicked.connect(btn_clicked_slot) if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
注:这时的它们不是实时显示字体的效果的 。
QFontDialog的信号:
总结:
下面看下一个对话框:颜色对话框 QColorDialog: https://www.cnblogs.com/zach0812/p/11392840.html