0

我有以下两个脚本,第一个是使用 pyuic4 for Windows 生成的,没有修改。

文本编辑.py:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'mytextedit.ui'
#
# Created: Mon Sep 22 14:47:34 2014
#      by: PyQt4 UI code generator 4.11.2
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_notepad(object):
    def setupUi(self, notepad):
        notepad.setObjectName(_fromUtf8("notepad"))
        notepad.resize(763, 638)
        self.centralwidget = QtGui.QWidget(notepad)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.verticalLayout_2 = QtGui.QVBoxLayout(self.centralwidget)
        self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
        self.verticalLayout = QtGui.QVBoxLayout()
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.tabWidget = QtGui.QTabWidget(self.centralwidget)
        self.tabWidget.setMaximumSize(QtCore.QSize(16777215, 16777215))
        self.tabWidget.setTabShape(QtGui.QTabWidget.Triangular)
        self.tabWidget.setTabsClosable(True)
        self.tabWidget.setMovable(True)
        self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
        self.tab = QtGui.QWidget()
        self.tab.setObjectName(_fromUtf8("tab"))
        self.verticalLayout_3 = QtGui.QVBoxLayout(self.tab)
        self.verticalLayout_3.setObjectName(_fromUtf8("verticalLayout_3"))
        self.plainTextEdit = QtGui.QPlainTextEdit(self.tab)
        self.plainTextEdit.setObjectName(_fromUtf8("plainTextEdit"))
        self.verticalLayout_3.addWidget(self.plainTextEdit)
        self.tabWidget.addTab(self.tab, _fromUtf8(""))
        self.tab_2 = QtGui.QWidget()
        self.tab_2.setObjectName(_fromUtf8("tab_2"))
        self.tabWidget.addTab(self.tab_2, _fromUtf8(""))
        self.verticalLayout.addWidget(self.tabWidget)
        self.verticalLayout_2.addLayout(self.verticalLayout)
        notepad.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(notepad)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 763, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        self.menuFile = QtGui.QMenu(self.menubar)
        self.menuFile.setObjectName(_fromUtf8("menuFile"))
        self.menuEdit = QtGui.QMenu(self.menubar)
        self.menuEdit.setObjectName(_fromUtf8("menuEdit"))
        notepad.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(notepad)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        notepad.setStatusBar(self.statusbar)
        self.actionNew = QtGui.QAction(notepad)
        self.actionNew.setObjectName(_fromUtf8("actionNew"))
        self.actionOpen = QtGui.QAction(notepad)
        self.actionOpen.setObjectName(_fromUtf8("actionOpen"))
        self.actionSave = QtGui.QAction(notepad)
        self.actionSave.setObjectName(_fromUtf8("actionSave"))
        self.actionSave_As = QtGui.QAction(notepad)
        self.actionSave_As.setObjectName(_fromUtf8("actionSave_As"))
        self.actionPaste = QtGui.QAction(notepad)
        self.actionPaste.setObjectName(_fromUtf8("actionPaste"))
        self.actionCut = QtGui.QAction(notepad)
        self.actionCut.setObjectName(_fromUtf8("actionCut"))
        self.actionCopy = QtGui.QAction(notepad)
        self.actionCopy.setObjectName(_fromUtf8("actionCopy"))
        self.actionDelete = QtGui.QAction(notepad)
        self.actionDelete.setObjectName(_fromUtf8("actionDelete"))
        self.actionDelete_Line = QtGui.QAction(notepad)
        self.actionDelete_Line.setObjectName(_fromUtf8("actionDelete_Line"))
        self.actionSelect_All_2 = QtGui.QAction(notepad)
        self.actionSelect_All_2.setObjectName(_fromUtf8("actionSelect_All_2"))
        self.menuFile.addAction(self.actionNew)
        self.menuFile.addAction(self.actionOpen)
        self.menuFile.addAction(self.actionSave)
        self.menuFile.addAction(self.actionSave_As)
        self.menuEdit.addAction(self.actionPaste)
        self.menuEdit.addAction(self.actionCut)
        self.menuEdit.addAction(self.actionCopy)
        self.menuEdit.addAction(self.actionDelete)
        self.menuEdit.addAction(self.actionDelete_Line)
        self.menuEdit.addAction(self.actionSelect_All_2)
        self.menubar.addAction(self.menuFile.menuAction())
        self.menubar.addAction(self.menuEdit.menuAction())

        self.retranslateUi(notepad)
        self.tabWidget.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(notepad)

    def retranslateUi(self, notepad):
        notepad.setWindowTitle(_translate("notepad", "Text Edit", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("notepad", "Tab 1", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("notepad", "Tab 2", None))
        self.menuFile.setTitle(_translate("notepad", "File", None))
        self.menuEdit.setTitle(_translate("notepad", "Edit", None))
        self.actionNew.setText(_translate("notepad", "New", None))
        self.actionNew.setShortcut(_translate("notepad", "Ctrl+N", None))
        self.actionOpen.setText(_translate("notepad", "Open", None))
        self.actionOpen.setShortcut(_translate("notepad", "Ctrl+O", None))
        self.actionSave.setText(_translate("notepad", "Save", None))
        self.actionSave.setShortcut(_translate("notepad", "Ctrl+S", None))
        self.actionSave_As.setText(_translate("notepad", "Save As", None))
        self.actionSave_As.setShortcut(_translate("notepad", "Ctrl+Shift+S", None))
        self.actionPaste.setText(_translate("notepad", "Paste", None))
        self.actionPaste.setShortcut(_translate("notepad", "Ctrl+V", None))
        self.actionCut.setText(_translate("notepad", "Cut", None))
        self.actionCopy.setText(_translate("notepad", "Copy", None))
        self.actionDelete.setText(_translate("notepad", "Delete", None))
        self.actionDelete_Line.setText(_translate("notepad", "Delete Line", None))
        self.actionSelect_All_2.setText(_translate("notepad", "Select All", None))

开始.py:

#! /usr/bin/env python3.4

import sys
from PyQt4 import QtCore, QtGui
from textedit import UI_notepad

class StartQT4(QtGui.QMainWindow):
    """docstring for StartQT4"""
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.parent = parent
        self.ui = UI_notepad()
        self.ui.setupUi(self)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    sys.exit(app.exec_())

我对导入的工作原理有相当好的理解,但无论我尝试什么,我总是会得到错误:

Traceback (most recent call last):
  File "E:\Python\Text Editor\start.py", line 5, in <module>
    from textedit import UI_notepad
ImportError: cannot import name 'UI_notepad'
4

1 回答 1

2

你需要一个小写i字母 from textedit import Ui_notepad

您正在使用from textedit import UI_notepad并且您的课程是:

class Ui_notepad(object)<-i不是I

于 2014-09-22T20:10:56.663 回答