When using Pyside or PYQT, it is often used to import the well-drawn UI files into the code, the following are two ways to dial in:
Import Pyqt4.uic from
PyQt4 import qtcore, Qtgui
mainwindowform, mainwindowbase = PyQt4.uic.loadUiType (' ui/ Mainwindow.ui ')
class MainWindow (Mainwindowbase, Mainwindowform):
def __init__ (self, parent = None):
Super (MainWindow, self). __init__ (parent)
# setup The UI
Self.setupui (self)
if (__name__ = = ' __main__ '): C8/>app = None
if (not app):
app = Qtgui.qapplication ([])
window = MainWindow ()
window.show ()
if (APP):
app.exec_ ()
The second type:
Import Pyqt4.uic from
PyQt4 import Qtcore, Qtgui
class MainWindow (Qtgui.qmainwindow):
def __init__ (self, Parent = None):
super (MainWindow, self). __init__ (parent)
# Load the UI
PyQt4.uic.loadUi (' ui/ Mainwindow.ui ', self)
if (__name__ = = ' __main__ '):
app = None
if app:
app = Qtgui.qapplication ([ ])
window = MainWindow ()
window.show ()
if (APP):
app.exec_ ()
Of course, if you use Pyside, we can write a method that specifically reads the UI file, returning BaseClass and Formclass to the classes in the following UI.
#-*-coding:utf-8-*-from pyside import Qtgui import xml.etree.ElementTree as XML from Cstrin
GIO Import Stringio def load_ui_file (ui_file, type= ' pyside '): if type = = ' pyside ': Import Pysideuic Print Pysideuic parsed = Xml.parse (ui_file) Widget_class = Parsed.find (' widget '). Get (' class ') for M_class = Parsed.find (' class '). Text with open (Ui_file, ' R ') as F:o = Stringio () frame =
{} pysideuic.compileui (f, O, indent=0) PYc = Compile (O.getvalue (), ' <string> ', ' exec ') EXEC PYc in frame form_class = frame[' ui_%s '% form_class] Base_class = GetAttr (Qtgui, WI Dget_class) return form_class, base_class elif type = = ' PyQt4 ': Import Pyqt4.uic return PYQT4 . Uic.loaduitype (ui_file) If __name__ = = "__main__": ui_file = ' Test.ui ' load_ui_file (ui_file)
This allows the pyside and PYQT to invoke the. ui file, and the performance of both methods and the memory used by someone has been specifically tested, but the author also recommends the first method, please see the link below for more information.
PyQt load. ui file