1

我设计了一个 GUI 并将 .ui 文件转换为 .py 文件,我发现了很多奇怪的乱码。无论如何,任何人都可以帮助我将此语法转换为常规 python,以便我实际上可以将函数传递给按钮等。过去一天我一直在玩它,并取得了一些成就。

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(929, 597)
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "Intex | Server Monitor System", None, QtGui.QApplication.UnicodeUTF8))
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.plottab = QtGui.QTabWidget(self.centralwidget)
        self.plottab.setGeometry(QtCore.QRect(20, 10, 661, 511))
        self.plottab.setObjectName(_fromUtf8("plottab"))
        self.juliet = QtGui.QWidget()
        self.juliet.setObjectName(_fromUtf8("juliet"))
        self.label_2 = QtGui.QLabel(self.juliet)
        self.label_2.setGeometry(QtCore.QRect(20, 10, 111, 16))
        self.label_2.setText(QtGui.QApplication.translate("MainWindow", "Performance Monitor", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.scrollArea_2 = QtGui.QScrollArea(self.juliet)
        self.scrollArea_2.setGeometry(QtCore.QRect(20, 30, 241, 221))
        self.scrollArea_2.setWidgetResizable(True)
        self.scrollArea_2.setObjectName(_fromUtf8("scrollArea_2"))
        self.scrollAreaWidgetContents_2 = QtGui.QWidget()
        self.scrollAreaWidgetContents_2.setGeometry(QtCore.QRect(0, 0, 237, 217))
        self.scrollAreaWidgetContents_2.setObjectName(_fromUtf8("scrollAreaWidgetContents_2"))
        self.scrollArea_2.setWidget(self.scrollAreaWidgetContents_2)
        self.label_13 = QtGui.QLabel(self.juliet)
        self.label_13.setGeometry(QtCore.QRect(330, 10, 111, 16))
        self.label_13.setText(QtGui.QApplication.translate("MainWindow", "Server Load Monitor", None, QtGui.QApplication.UnicodeUTF8))
        self.label_13.setObjectName(_fromUtf8("label_13"))
        self.scrollArea_10 = QtGui.QScrollArea(self.juliet)
        self.scrollArea_10.setGeometry(QtCore.QRect(330, 30, 241, 221))
        self.scrollArea_10.setWidgetResizable(True)
        self.scrollArea_10.setObjectName(_fromUtf8("scrollArea_10"))
        self.scrollAreaWidgetContents_10 = QtGui.QWidget()
        self.scrollAreaWidgetContents_10.setGeometry(QtCore.QRect(0, 0, 237, 217))
        self.scrollAreaWidgetContents_10.setObjectName(_fromUtf8("scrollAreaWidgetContents_10"))
        self.scrollArea_10.setWidget(self.scrollAreaWidgetContents_10)
        self.label_16 = QtGui.QLabel(self.juliet)
        self.label_16.setGeometry(QtCore.QRect(20, 270, 171, 16))
        self.label_16.setText(QtGui.QApplication.translate("MainWindow", "Incoming IP Adresses/Users", None, QtGui.QApplication.UnicodeUTF8))
        self.label_16.setObjectName(_fromUtf8("label_16"))
        self.scrollArea_13 = QtGui.QScrollArea(self.juliet)
        self.scrollArea_13.setGeometry(QtCore.QRect(20, 290, 241, 181))
        self.scrollArea_13.setWidgetResizable(True)
        self.scrollArea_13.setObjectName(_fromUtf8("scrollArea_13"))
        self.scrollAreaWidgetContents_13 = QtGui.QWidget()
        self.scrollAreaWidgetContents_13.setGeometry(QtCore.QRect(0, 0, 237, 177))
        self.scrollAreaWidgetContents_13.setObjectName(_fromUtf8("scrollAreaWidgetContents_13"))
        self.scrollArea_13.setWidget(self.scrollAreaWidgetContents_13)
        self.plottab.addTab(self.juliet, _fromUtf8(""))
        self.newjulietleft = QtGui.QWidget()
        self.newjulietleft.setObjectName(_fromUtf8("newjulietleft"))
        self.scrollArea = QtGui.QScrollArea(self.newjulietleft)
        self.scrollArea.setGeometry(QtCore.QRect(20, 30, 241, 221))
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
        self.scrollAreaWidgetContents = QtGui.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 237, 217))
        self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        self.label = QtGui.QLabel(self.newjulietleft)
        self.label.setGeometry(QtCore.QRect(20, 10, 111, 16))
        self.label.setText(QtGui.QApplication.translate("MainWindow", "Performance Monitor", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_12 = QtGui.QLabel(self.newjulietleft)
        self.label_12.setGeometry(QtCore.QRect(330, 10, 111, 16))
        self.label_12.setText(QtGui.QApplication.translate("MainWindow", "Server Load Monitor", None, QtGui.QApplication.UnicodeUTF8))
        self.label_12.setObjectName(_fromUtf8("label_12"))
        self.scrollArea_9 = QtGui.QScrollArea(self.newjulietleft)
        self.scrollArea_9.setGeometry(QtCore.QRect(330, 30, 241, 221))
        self.scrollArea_9.setWidgetResizable(True)
        self.scrollArea_9.setObjectName(_fromUtf8("scrollArea_9"))
        self.scrollAreaWidgetContents_9 = QtGui.QWidget()
        self.scrollAreaWidgetContents_9.setGeometry(QtCore.QRect(0, 0, 237, 217))
        self.scrollAreaWidgetContents_9.setObjectName(_fromUtf8("scrollAreaWidgetContents_9"))
        self.scrollArea_9.setWidget(self.scrollAreaWidgetContents_9)
        self.label_15 = QtGui.QLabel(self.newjulietleft)
        self.label_15.setGeometry(QtCore.QRect(20, 270, 171, 16))
        self.label_15.setText(QtGui.QApplication.translate("MainWindow", "Incoming IP Adresses/Users", None, QtGui.QApplication.UnicodeUTF8))
        self.label_15.setObjectName(_fromUtf8("label_15"))
        self.scrollArea_12 = QtGui.QScrollArea(self.newjulietleft)
        self.scrollArea_12.setGeometry(QtCore.QRect(20, 290, 241, 181))
        self.scrollArea_12.setWidgetResizable(True)
        self.scrollArea_12.setObjectName(_fromUtf8("scrollArea_12"))
        self.scrollAreaWidgetContents_12 = QtGui.QWidget()
        self.scrollAreaWidgetContents_12.setGeometry(QtCore.QRect(0, 0, 237, 177))
        self.scrollAreaWidgetContents_12.setObjectName(_fromUtf8("scrollAreaWidgetContents_12"))
        self.scrollArea_12.setWidget(self.scrollAreaWidgetContents_12)
        self.plottab.addTab(self.newjulietleft, _fromUtf8(""))
        self.custom = QtGui.QWidget()
        self.custom.setObjectName(_fromUtf8("custom"))
        self.comboBox = QtGui.QComboBox(self.custom)
        self.comboBox.setGeometry(QtCore.QRect(20, 30, 74, 22))
        self.comboBox.setObjectName(_fromUtf8("comboBox"))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.setItemText(0, QtGui.QApplication.translate("MainWindow", "Hulk", None, QtGui.QApplication.UnicodeUTF8))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.setItemText(1, QtGui.QApplication.translate("MainWindow", "Superman", None, QtGui.QApplication.UnicodeUTF8))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.setItemText(2, QtGui.QApplication.translate("MainWindow", "Karma", None, QtGui.QApplication.UnicodeUTF8))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.setItemText(3, QtGui.QApplication.translate("MainWindow", "wdl", None, QtGui.QApplication.UnicodeUTF8))
        self.comboBox.addItem(_fromUtf8(""))
        self.comboBox.setItemText(4, QtGui.QApplication.translate("MainWindow", "Pong", None, QtGui.QApplication.UnicodeUTF8))
        self.label_3 = QtGui.QLabel(self.custom)
        self.label_3.setGeometry(QtCore.QRect(20, 10, 71, 16))
        self.label_3.setText(QtGui.QApplication.translate("MainWindow", "Select Server", None, QtGui.QApplication.UnicodeUTF8))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.scrollArea_7 = QtGui.QScrollArea(self.custom)
        self.scrollArea_7.setGeometry(QtCore.QRect(20, 100, 241, 221))
        self.scrollArea_7.setWidgetResizable(True)
        self.scrollArea_7.setObjectName(_fromUtf8("scrollArea_7"))
        self.scrollAreaWidgetContents_7 = QtGui.QWidget()
        self.scrollAreaWidgetContents_7.setGeometry(QtCore.QRect(0, 0, 237, 217))
        self.scrollAreaWidgetContents_7.setObjectName(_fromUtf8("scrollAreaWidgetContents_7"))
        self.scrollArea_7.setWidget(self.scrollAreaWidgetContents_7)
        self.label_10 = QtGui.QLabel(self.custom)
        self.label_10.setGeometry(QtCore.QRect(20, 80, 111, 16))
        self.label_10.setText(QtGui.QApplication.translate("MainWindow", "Performance Monitor", None, QtGui.QApplication.UnicodeUTF8))
        self.label_10.setObjectName(_fromUtf8("label_10"))
        self.scrollArea_8 = QtGui.QScrollArea(self.custom)
        self.scrollArea_8.setGeometry(QtCore.QRect(360, 100, 241, 221))
        self.scrollArea_8.setWidgetResizable(True)
        self.scrollArea_8.setObjectName(_fromUtf8("scrollArea_8"))
        self.scrollAreaWidgetContents_8 = QtGui.QWidget()
        self.scrollAreaWidgetContents_8.setGeometry(QtCore.QRect(0, 0, 237, 217))
        self.scrollAreaWidgetContents_8.setObjectName(_fromUtf8("scrollAreaWidgetContents_8"))
        self.scrollArea_8.setWidget(self.scrollAreaWidgetContents_8)
        self.label_11 = QtGui.QLabel(self.custom)
        self.label_11.setGeometry(QtCore.QRect(360, 80, 111, 16))
        self.label_11.setText(QtGui.QApplication.translate("MainWindow", "Server Load Monitor", None, QtGui.QApplication.UnicodeUTF8))
        self.label_11.setObjectName(_fromUtf8("label_11"))
        self.label_14 = QtGui.QLabel(self.custom)
        self.label_14.setGeometry(QtCore.QRect(20, 340, 171, 16))
        self.label_14.setText(QtGui.QApplication.translate("MainWindow", "Incoming IP Adresses/Users", None, QtGui.QApplication.UnicodeUTF8))
        self.label_14.setObjectName(_fromUtf8("label_14"))
        self.scrollArea_11 = QtGui.QScrollArea(self.custom)
        self.scrollArea_11.setGeometry(QtCore.QRect(20, 360, 241, 111))
        self.scrollArea_11.setWidgetResizable(True)
        self.scrollArea_11.setObjectName(_fromUtf8("scrollArea_11"))
        self.scrollAreaWidgetContents_11 = QtGui.QWidget()
        self.scrollAreaWidgetContents_11.setGeometry(QtCore.QRect(0, 0, 237, 107))
        self.scrollAreaWidgetContents_11.setObjectName(_fromUtf8("scrollAreaWidgetContents_11"))
        self.scrollArea_11.setWidget(self.scrollAreaWidgetContents_11)
        self.plottab.addTab(self.custom, _fromUtf8(""))
        self.tab = QtGui.QWidget()
        self.tab.setObjectName(_fromUtf8("tab"))
        self.plottab.addTab(self.tab, _fromUtf8(""))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(510, 530, 75, 23))
        self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Refresh", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(600, 530, 75, 23))
        self.pushButton_2.setText(QtGui.QApplication.translate("MainWindow", "Exit", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.progressBar = QtGui.QProgressBar(self.centralwidget)
        self.progressBar.setGeometry(QtCore.QRect(20, 530, 118, 23))
        self.progressBar.setProperty("value", 24)
        self.progressBar.setObjectName(_fromUtf8("progressBar"))
        self.scrollArea_3 = QtGui.QScrollArea(self.centralwidget)
        self.scrollArea_3.setGeometry(QtCore.QRect(710, 30, 191, 80))
        self.scrollArea_3.setWidgetResizable(True)
        self.scrollArea_3.setObjectName(_fromUtf8("scrollArea_3"))
        self.scrollAreaWidgetContents_3 = QtGui.QWidget()
        self.scrollAreaWidgetContents_3.setGeometry(QtCore.QRect(0, 0, 187, 76))
        self.scrollAreaWidgetContents_3.setObjectName(_fromUtf8("scrollAreaWidgetContents_3"))
        self.scrollArea_3.setWidget(self.scrollAreaWidgetContents_3)
        self.label_17 = QtGui.QLabel(self.centralwidget)
        self.label_17.setGeometry(QtCore.QRect(710, 10, 171, 16))
        self.label_17.setText(QtGui.QApplication.translate("MainWindow", "Alerts", None, QtGui.QApplication.UnicodeUTF8))
        self.label_17.setObjectName(_fromUtf8("label_17"))
        self.label_4 = QtGui.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(710, 130, 46, 13))
        self.label_4.setText(QtGui.QApplication.translate("MainWindow", "Plots", None, QtGui.QApplication.UnicodeUTF8))
        self.label_4.setObjectName(_fromUtf8("label_4"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 929, 18))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        self.menuFile = QtGui.QMenu(self.menubar)
        self.menuFile.setTitle(QtGui.QApplication.translate("MainWindow", "File", None, QtGui.QApplication.UnicodeUTF8))
        self.menuFile.setObjectName(_fromUtf8("menuFile"))
        self.menuEdit = QtGui.QMenu(self.menubar)
        self.menuEdit.setTitle(QtGui.QApplication.translate("MainWindow", "Edit", None, QtGui.QApplication.UnicodeUTF8))
        self.menuEdit.setObjectName(_fromUtf8("menuEdit"))
        self.menuAbout = QtGui.QMenu(self.menubar)
        self.menuAbout.setTitle(QtGui.QApplication.translate("MainWindow", "Help", None, QtGui.QApplication.UnicodeUTF8))
        self.menuAbout.setObjectName(_fromUtf8("menuAbout"))
        self.menuView = QtGui.QMenu(self.menubar)
        self.menuView.setTitle(QtGui.QApplication.translate("MainWindow", "View", None, QtGui.QApplication.UnicodeUTF8))
        self.menuView.setObjectName(_fromUtf8("menuView"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)
        self.actionSave_Data = QtGui.QAction(MainWindow)
        self.actionSave_Data.setEnabled(False)
        self.actionSave_Data.setText(QtGui.QApplication.translate("MainWindow", "Save Data ", None, QtGui.QApplication.UnicodeUTF8))
        self.actionSave_Data.setShortcut(QtGui.QApplication.translate("MainWindow", "Ctrl+S", None, QtGui.QApplication.UnicodeUTF8))
        self.actionSave_Data.setObjectName(_fromUtf8("actionSave_Data"))
        self.actionPrint = QtGui.QAction(MainWindow)
        self.actionPrint.setText(QtGui.QApplication.translate("MainWindow", "Print            ", None, QtGui.QApplication.UnicodeUTF8))
        self.actionPrint.setShortcut(QtGui.QApplication.translate("MainWindow", "Ctrl+P", None, QtGui.QApplication.UnicodeUTF8))
        self.actionPrint.setObjectName(_fromUtf8("actionPrint"))
        self.actionExit = QtGui.QAction(MainWindow)
        self.actionExit.setText(QtGui.QApplication.translate("MainWindow", "Exit", None, QtGui.QApplication.UnicodeUTF8))
        self.actionExit.setObjectName(_fromUtf8("actionExit"))
        self.actionExit_2 = QtGui.QAction(MainWindow)
        self.actionExit_2.setText(QtGui.QApplication.translate("MainWindow", "Refresh", None, QtGui.QApplication.UnicodeUTF8))
        self.actionExit_2.setShortcut(QtGui.QApplication.translate("MainWindow", "Ctrl+R", None, QtGui.QApplication.UnicodeUTF8))
        self.actionExit_2.setObjectName(_fromUtf8("actionExit_2"))
        self.actionExit_3 = QtGui.QAction(MainWindow)
        self.actionExit_3.setText(QtGui.QApplication.translate("MainWindow", "Exit", None, QtGui.QApplication.UnicodeUTF8))
        self.actionExit_3.setShortcut(QtGui.QApplication.translate("MainWindow", "Ctrl+E", None, QtGui.QApplication.UnicodeUTF8))
        self.actionExit_3.setObjectName(_fromUtf8("actionExit_3"))
        self.actionCopy = QtGui.QAction(MainWindow)
        self.actionCopy.setText(QtGui.QApplication.translate("MainWindow", "Copy", None, QtGui.QApplication.UnicodeUTF8))
        self.actionCopy.setShortcut(QtGui.QApplication.translate("MainWindow", "Ctrl+C", None, QtGui.QApplication.UnicodeUTF8))
        self.actionCopy.setObjectName(_fromUtf8("actionCopy"))
        self.actionCut = QtGui.QAction(MainWindow)
        self.actionCut.setText(QtGui.QApplication.translate("MainWindow", "Cut", None, QtGui.QApplication.UnicodeUTF8))
        self.actionCut.setShortcut(QtGui.QApplication.translate("MainWindow", "Ctrl+Shift+C", None, QtGui.QApplication.UnicodeUTF8))
        self.actionCut.setObjectName(_fromUtf8("actionCut"))
        self.actionPaste = QtGui.QAction(MainWindow)
        self.actionPaste.setText(QtGui.QApplication.translate("MainWindow", "Paste", None, QtGui.QApplication.UnicodeUTF8))
        self.actionPaste.setShortcut(QtGui.QApplication.translate("MainWindow", "Ctrl+P", None, QtGui.QApplication.UnicodeUTF8))
        self.actionPaste.setObjectName(_fromUtf8("actionPaste"))
        self.actionContact_Us = QtGui.QAction(MainWindow)
        self.actionContact_Us.setText(QtGui.QApplication.translate("MainWindow", "Contact Us", None, QtGui.QApplication.UnicodeUTF8))
        self.actionContact_Us.setObjectName(_fromUtf8("actionContact_Us"))
        self.actionAbout = QtGui.QAction(MainWindow)
        self.actionAbout.setText(QtGui.QApplication.translate("MainWindow", "About", None, QtGui.QApplication.UnicodeUTF8))
        self.actionAbout.setObjectName(_fromUtf8("actionAbout"))
        self.actionDocumentation = QtGui.QAction(MainWindow)
        self.actionDocumentation.setText(QtGui.QApplication.translate("MainWindow", "Documentation", None, QtGui.QApplication.UnicodeUTF8))
        self.actionDocumentation.setObjectName(_fromUtf8("actionDocumentation"))
        self.actionPython_Interpreter = QtGui.QAction(MainWindow)
        self.actionPython_Interpreter.setText(QtGui.QApplication.translate("MainWindow", "Python Interpreter", None, QtGui.QApplication.UnicodeUTF8))
        self.actionPython_Interpreter.setObjectName(_fromUtf8("actionPython_Interpreter"))
        self.menuFile.addAction(self.actionSave_Data)
        self.menuFile.addAction(self.actionPrint)
        self.menuFile.addAction(self.actionExit_2)
        self.menuFile.addSeparator()
        self.menuFile.addAction(self.actionExit_3)
        self.menuEdit.addAction(self.actionCopy)
        self.menuEdit.addAction(self.actionCut)
        self.menuEdit.addAction(self.actionPaste)
        self.menuAbout.addAction(self.actionContact_Us)
        self.menuAbout.addSeparator()
        self.menuAbout.addAction(self.actionAbout)
        self.menuAbout.addSeparator()
        self.menuAbout.addAction(self.actionDocumentation)
        self.menuView.addAction(self.actionPython_Interpreter)
        self.menubar.addAction(self.menuFile.menuAction())
        self.menubar.addAction(self.menuEdit.menuAction())
        self.menubar.addAction(self.menuView.menuAction())
        self.menubar.addAction(self.menuAbout.menuAction())

        self.retranslateUi(MainWindow)
        self.plottab.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        self.plottab.setTabText(self.plottab.indexOf(self.juliet), QtGui.QApplication.translate("MainWindow", "Juliet", None, QtGui.QApplication.UnicodeUTF8))
        self.plottab.setTabText(self.plottab.indexOf(self.newjulietleft), QtGui.QApplication.translate("MainWindow", "Newjulietleft", None, QtGui.QApplication.UnicodeUTF8))
        self.plottab.setTabText(self.plottab.indexOf(self.custom), QtGui.QApplication.translate("MainWindow", "Custom", None, QtGui.QApplication.UnicodeUTF8))
        self.plottab.setTabText(self.plottab.indexOf(self.tab), QtGui.QApplication.translate("MainWindow", "Overall Analasys", None, QtGui.QApplication.UnicodeUTF8)

)

4

1 回答 1

4

您不应该更改此文件。它是一个生成的文件。

您需要做的是创建一个继承自Ui_MainWindow.

创建一个新模块并执行以下操作:

import sys
from PyQt4 import QtCore, QtGui
import your_generated_module

class MyMain(QtGui.QMainWindow, your_generated_module.Ui_MainWindow):
    def __init__(self, parent=None):
        super(MyMain, self).__init__(parent)
        self.setupUi(self)  
        # example of an event - pushbutton
        self.connect(self.pushButton, 
                     QtCore.SIGNAL("clicked()"),
                     self.test_print)

    def test_print():
        print "It worked!"    

def main():
   app = QtGui.QApplication(sys.argv)
   main_window = MyMain()
   main_window.show()
   app.exec_()

if __name__ == '__main__':
   main()

现在,如果您运行此代码,您将看到您的主窗口。单击按钮将打印“它工作!” 在标准输出(控制台)。

于 2012-07-19T22:02:29.663 回答