我正在使用PyQt4编写一个小应用程序。我需要更改html文件的字体,以便如何从已安装字体列表中选择字体名称? 我的主窗口中有一个工具按钮,它连接到下面的功能,但每次运行我的程序而不是主窗口时,都会出现字体对话框:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
...
QtCore.QObject.connect(self.toolButton, QtCore.SIGNAL("clicked()"), self.fontPicker())
...
def fontPicker(self):
f, ok = QtGui.QFontDialog.getFont()
if ok:
...
def main(dir_in):
app = QtGui.QApplication(sys.argv)
form = QtGui.QMainWindow()
myapp = Ui_MainWindow()
myapp.setupUi(form)
form.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
我认为当你想在另一个类中定义Widgets,Layouts时,将使用你的代码。 例如:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
# In the class Ui_SideWindow you can define widgets, signals, slots, ect.
class Ui_SideWindow(object):
def setupUi(self, MainWindow):
self.label_sidewindow = QLabel()
self.label_sidewindow.setText("I am defined in another class!")
# The class "Mainwindow" inherit the defined widgets, ect. from Ui_SideWindow,
# if you add the class in the definition, so class name(BaseClassName)
class MainWindow(QMainWindow, Ui_SideWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
# call setupUI
self.setupUi(self)
self.setWindowTitle("Get Font")
self.button = QPushButton()
self.button.setText(" Get Font")
self.label = QLabel()
self.layout = QVBoxLayout()
self.layout.addWidget(self.button)
self.layout.addWidget(self.label)
self.layout.addWidget(self.label_sidewindow) # defined in class Ui_SideWindow
self.main_frame = QWidget()
self.main_frame.setLayout(self.layout)
self.setCentralWidget(self.main_frame)
#####################
# Signals and Slots #
#####################
self.connect(self.button, SIGNAL("clicked()"), self.getfont)
def getfont(self):
item, ok = QFontDialog.getFont()
if ok is True:
self.label.setText(item.toString())
else:
pass
def main():
app = QApplication(sys.argv)
form = MainWindow()
form.show()
app.exec_()
if __name__ == "__main__":
main()
我希望我能帮到你!