如何在文本编辑器中使用PyQt的Font Widget?

时间:2018-04-02 17:32:11

标签: python pyqt pyqt4 qtextedit

我在文本编辑器中添加使用PyQt4中的Font Widget时遇到了麻烦。当我尝试单击“字体”选项时,它会出现此错误:

文件“目录”,第230行,在FontFamily中     self.text.setCurrentFont(字体) TypeError:setCurrentFont(self,QFont):参数1具有意外类型'bool'

当我尝试单击“字体大小”选项时,它会出现此错误:

QFont :: setPointSizeF:点大小< = 0(0.000000),必须大于0

这是我的代码:

#Imports
import sys, os
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QApplication, QColumnView, QFileSystemModel, QSplitter, QTreeView
from PyQt4.QtCore import Qt, QDir


class Window(QtGui.QMainWindow):

    #Window Settings
    def __init__(self):
        super(Window, self).__init__()
        self.showMaximized()
        self.setWindowTitle("Editory")
        self.setWindowIcon(QtGui.QIcon('icons/favicon.png'))

        #Text Window
        self.text = QtGui.QTextEdit(self)
        self.text.setTabStopWidth(12)
        self.setCentralWidget(self.text)

        # Font variables
        fontBox = QtGui.QFontComboBox(self)
        fontBox.currentFontChanged.connect(self.FontFamily)

        fontSize = QtGui.QComboBox(self)
        fontSize.setEditable(True)

        # Minimum number of chars displayed
        fontSize.setMinimumContentsLength(3)

        fontSize.activated.connect(self.FontSize)

        # Font Sizes
        fontSizes = ['6','7','8','9','10','11','12','13','14',
             '15','16','18','20','22','24','26','28',
             '32','36','40','44','48','54','60','66',
             '72','80','88','96']

        for i in fontSizes:
            fontSize.addItem(i)

        #New Input
        new = QtGui.QAction("&New", self)
        new.setShortcut("Ctrl+N")
        new.triggered.connect(self.New)

        #Open Input
        open = QtGui.QAction("&Open", self)
        open.setShortcut("Ctrl+O")
        open.triggered.connect(self.Open)

        #Save Input
        save = QtGui.QAction("&Save", self)
        save.setShortcut("Ctrl+S")
        save.triggered.connect(self.Save)

        #Print Input
        prints = QtGui.QAction("&Print", self)
        prints.setShortcut("Ctrl+P")
        prints.triggered.connect(self.Print)

        #Quit Input
        quit = QtGui.QAction("&Quit", self)
        quit.setShortcut("Ctrl+Q")
        quit.triggered.connect(self.Quit)

        self.statusBar()

        #Menubar
        menubar = self.menuBar()

        #File Menu
        file = menubar.addMenu('&File')

        #File Inputs
        file.addAction(new)
        file.addAction(open)
        file.addAction(save)
        file.addAction(prints)
        file.addSeparator()
        file.addAction(quit)

        #Cut Input
        cut = QtGui.QAction("&Cut", self)
        cut.setShortcut("Ctrl+X")
        cut.triggered.connect(self.Cut)

        #Copy Input
        copy = QtGui.QAction("&Copy", self)
        copy.setShortcut("Ctrl+C")
        copy.triggered.connect(self.Copy)

        #Paste Input
        paste = QtGui.QAction("&Paste", self)
        paste.setShortcut("Ctrl+V")
        paste.triggered.connect(self.Paste)

        #Undo Input
        undo = QtGui.QAction("&Undo", self)
        undo.setShortcut("Ctrl+Z")
        undo.triggered.connect(self.Undo)

        #Redo Input
        redo = QtGui.QAction("&Redo", self)
        redo.setShortcut("Ctrl+Y")
        redo.triggered.connect(self.Redo)

        #Edit Menubar
        edit = menubar.addMenu('&Edit')

        #Edit Inputs
        edit.addAction(cut)
        edit.addAction(copy)
        edit.addAction(paste)
        edit.addSeparator()
        edit.addAction(undo)
        edit.addAction(redo)

        #Fullscreen Input
        fullscreen = QtGui.QAction("&Fullscreen", self)
        fullscreen.setShortcut("F11")
        fullscreen.triggered.connect(self.Fullscreen)

        #Align Left Input
        align_left = QtGui.QAction("&Align Left", self)
        align_left.triggered.connect(self.Align_Left)

        #Align Right Input
        align_right = QtGui.QAction("&Align Right", self)
        align_right.triggered.connect(self.Align_Right)

        #Align Center Input
        align_center = QtGui.QAction("&Align Center", self)
        align_center.triggered.connect(self.Align_Center)

        #Align Justify Input
        align_justify = QtGui.QAction("&Align Justify", self)
        align_justify.triggered.connect(self.Align_Justify)

        #View Menubar
        view = menubar.addMenu('&View')

        #View Inputs
        view.addAction(fullscreen)
        view.addSeparator()
        view.addAction(align_left)
        view.addAction(align_right)
        view.addAction(align_center)
        view.addAction(align_justify)

        #Font Family Input
        font_family = QtGui.QAction("&Font", self)
        font_family.triggered.connect(self.FontFamily)

        #Font Size Input
        font_size = QtGui.QAction("&Font Size", self)
        font_size.triggered.connect(self.FontSize)

        #Settings Menubar
        settings = menubar.addMenu('&Settings')

        #Settings Inputs
        settings.addAction(font_family)
        settings.addAction(font_size)

        #Imports Toolbar
        self.toolbar()

    #Input Functions
    def New(self):
        self.text.clear()

    def Open(self):
        filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File')
        f = open(filename, 'r')
        filedata = f.read()
        self.text.setText(filedata)
        f.close()

    def Save(self):
        name = QtGui.QFileDialog.getSaveFileName(self, 'Save File')
        file = open(name,'w')
        text = self.textEdit.toPlainText()
        file.write(text)
        file.close()

    def Print(self):
        print_dialog = QtGui.QPrintDialog()
        if print_dialog.exec_() == QtGui.QDialog.Accepted:
            self.text.document().print_(print_dialog.printer())

    def Quit(self):
        sys.exit()

    def Undo(self):
        self.text.undo()

    def Redo(self):
        self.text.redo()

    def Cut(self):
        self.text.cut()

    def Copy(self):
        self.text.copy()

    def Paste(self):
        self.text.paste()

    def Align_Left(self):
        self.text.setAlignment(Qt.AlignLeft)

    def Align_Right(self):
        self.text.setAlignment(Qt.AlignRight)

    def Align_Center(self):
        self.text.setAlignment(Qt.AlignCenter)

    def Align_Justify(self):
        self.text.setAlignment(Qt.AlignJustify)

    def Fullscreen(self):
        if not self.isFullScreen():
            self.showFullScreen()
        else:
            self.showMaximized()

    def FontFamily(self,font):
        self.text.setCurrentFont(font)

    def FontSize(self, fontsize):
        self.text.setFontPointSize(int(fontsize))

    #Toolbar
    def toolbar(self):

        #New Tool
        new = QtGui.QAction(QtGui.QIcon('icons/new.png'), 'New', self)
        new.triggered.connect(self.New)

        #Open Tool
        open = QtGui.QAction(QtGui.QIcon('icons/open.png'), 'Open', self)
        open.triggered.connect(self.Open)

        #Save Tool
        save = QtGui.QAction(QtGui.QIcon('icons/save.png'), 'Save', self)
        save.triggered.connect(self.Save)

        #Print Tool
        prints = QtGui.QAction(QtGui.QIcon('icons/print.png'), 'Print', self)
        prints.triggered.connect(self.Print)

        #Quit Tool
        quit = QtGui.QAction(QtGui.QIcon('icons/quit.png'), 'Quit', self)
        quit.triggered.connect(self.Quit)

        #Cut Tool
        cut = QtGui.QAction(QtGui.QIcon('icons/cut.png'), 'Cut', self)
        cut.triggered.connect(self.Cut)

        #Copy Tool
        copy = QtGui.QAction(QtGui.QIcon('icons/copy.png'), 'Copy', self)
        copy.triggered.connect(self.Copy)

        #Paste Tool
        paste = QtGui.QAction(QtGui.QIcon('icons/paste.png'), 'Paste', self)
        paste.triggered.connect(self.Paste)

        #Undo Tool
        undo = QtGui.QAction(QtGui.QIcon('icons/undo.png'), 'Undo', self)
        undo.triggered.connect(self.Undo)

        #Redo Tool
        redo = QtGui.QAction(QtGui.QIcon('icons/redo.png'), 'Redo', self)
        redo.triggered.connect(self.Redo)

        #Toolbar Menu
        self.toolbar = self.addToolBar("Toolbar")
        self.toolbar.addAction(new)
        self.toolbar.addAction(open)
        self.toolbar.addAction(save)
        self.toolbar.addSeparator()
        self.toolbar.addAction(cut)
        self.toolbar.addAction(copy)
        self.toolbar.addAction(paste)
        self.toolbar.addSeparator()
        self.toolbar.addAction(undo)
        self.toolbar.addAction(redo)
        self.toolbar.addSeparator()
        self.toolbar.addAction(prints)
        self.toolbar.addSeparator()
        self.toolbar.addAction(quit)

        #Reveals The Toolbar
        self.show()

#Run Function
def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())


run()

1 个答案:

答案 0 :(得分:1)

问题是,您最初是否正确连接到QFontComboBoxQComBoBox

...
fontBox.currentFontChanged.connect(self.FontFamily)
...
fontSize.activated.connect(self.FontSize)

然后用相同的函数连接触发信号,由于从未看到组合框,只发出触发信号:

font_family = QtGui.QAction("&Font", self)
font_family.triggered.connect(self.FontFamily)

#Font Size Input
font_size = QtGui.QAction("&Font Size", self)
font_size.triggered.connect(self.FontSize)

你必须做的是设置菜单必须有2个子菜单​​,分别打开QFontComboBoxQComboBox小部件,但是要在子菜单中包含这些小部件,你必须使用{{1 }}:

QWidgetAction