QtGui.QFontDialog

The QtGui.QFontDialog is a dialog widget for selecting a font.

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial 

In this example, we select a font name
and change the font of a label. 

author: Jan Bodnar
website: zetcode.com
last edited: October 2011
"""

import sys
from PyQt4 import QtGui

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):      

        vbox = QtGui.QVBoxLayout()

        btn = QtGui.QPushButton(‘Dialog‘, self)
        btn.setSizePolicy(QtGui.QSizePolicy.Fixed,
            QtGui.QSizePolicy.Fixed)

        btn.move(20, 20)

        vbox.addWidget(btn)

        btn.clicked.connect(self.showDialog)

        self.lbl = QtGui.QLabel(‘Knowledge only matters‘, self)
        self.lbl.move(130, 20)

        vbox.addWidget(self.lbl)
        self.setLayout(vbox)          

        self.setGeometry(300, 300, 250, 180)
        self.setWindowTitle(‘Font dialog‘)
        self.show()

    def showDialog(self):

        font, ok = QtGui.QFontDialog.getFont()
        if ok:
            self.lbl.setFont(font)

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == ‘__main__‘:
    main()

In our example, we have a button and a label. With QtGui.QFontDialog, we change the font of the label.

font, ok = QtGui.QFontDialog.getFont()

Here we pop up the font dialog. The getFont() method returns the font name and the ok parameter. It is equal to True if the user clicked OK; otherwise it is False.

if ok:
    self.label.setFont(font)

If we clicked ok, the font of the label would be changed.

时间: 2025-01-01 21:05:29

QtGui.QFontDialog的相关文章

PyQt4字体对话框QFontDialog

字体对话框时用来显示字体的对话框部件. #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 import QtGui, QtCore class FontDialog(QtGui.QWidget): def __init__(self, parent = None): QtGui.QWidget.__init__(self) hbox = QtGui.QHBoxLayout() self.setGeometry(300,

ZetCode PyQt4 tutorial Dialogs

#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we receive data from a QtGui.QInputDialog dialog. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from PyQ

Qt: 内建对话框(各种对话框都有了,且用到了qobject_cast解析sender的技术)

#include "BuiltinDialog.h" #include <QtGui/QTextEdit> #include <QtGui/QPushButton> #include <QtGui/QFileDialog> #include <QtGui/QFontDialog> #include <QtGui/QColorDialog> #include <QtGui/QPrintDialog> #include

关于Dialog----玲琅满目,任君挑选

下面来记录一下Qt中的形形色色的Dialg.... 直接给出PySide自带的example,因为我觉得我的改写也只是“依葫芦画瓢”...放心,Qt是开源的,真伟大.. #!/usr/bin/env python """PyQt4 port of the dialogs/standarddialogs example from Qt v4.x""" # This is only needed for Python v2 but is harmle

PyQt5学习笔记10----QColorDialog与QFontDialog

QColorDialog颜色对话框 QFontDialog字体对话框 from PyQt5 import QtWidgets from PyQt5.QtWidgets import QColorDialog,QFontDialog from PyQt5.QtCore import Qt from PyQt5.QtGui import QPalette class MyWindow(QtWidgets.QWidget): def __init__(self): super(MyWindow,sel

【2017-06-17】QtGui基础控件:QSpinBox及QDoubleSpinBox

今天开始一个新的系列,主要是翻译并摘录QtGui中基础空间的常用方法,并做一些简单的实验程序: 我觉得这是一个炒冷饭的行为,但有时候冷饭不能不炒,不热不好吃,而且也很容易发霉. 其实到现在这种状态,对控件所提供的方法是否熟练已经不是问题,因为我们可以经常去看Manual,更为重要的是程序的框架和数据结构,如何把数据有效组织起来,如何组合或自定义控件来实现我们想要的功能,可能更是一种能力的体现,编程嘛,主要是来解决实际问题的. 希望能通过这个系列,我能对Qt的控件.实现方法有一个更深入的理解. Q

QtGui.QPixmap

A QtGui.QPixmap is one of the widgets used to work with images. It is optimized for showing images on screen. In our code example, we will use the QtGui.QPixmap to display an image on the window. #!/usr/bin/python # -*- coding: utf-8 -*- """

QtGui.QLineEdit

A QtGui.QLineEdit is a widget that allows to enter and edit a single line of plain text. There are undo and redo, cut and paste, and drag & drop functions available for the widget. #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode P

QtGui.QSplitter

A QtGui.QSplitter lets the user control the size of child widgets by dragging the boundary between the children. In our example, we show three QtGui.QFrame widgets organized with two splitters. #!/usr/bin/python # -*- coding: utf-8 -*- """