Two ways to load UI files Pyside and PYQT

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.