Reputation: 43
I am attempting to load the .ui file that I created. However, when I run my python app, the main window is empty. I believe that the issue is in the line where I am loading the actual file. In all of the examples that I have seen, you are able to just load in the file, if the .py file is in the same directory. However, this wasn't working for me, so I had to use the absolute path. This fixed the FileNotFoundError that I was getting, but now I have this issue.
from PyQt5 import QtWidgets, uic
import sys
import os
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
ui_path = os.path.dirname(os.path.abspath(__file__))
uic.loadUiType(os.path.join(ui_path, "myfile.ui"))[0]
self.show()
app = QtWidgets.QApplication(sys.argv)
window = Ui()
app.exec_()
Myfile.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1141</width>
<height>885</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QFrame" name="sidebar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>200</width>
<height>720</height>
</rect>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>720</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(209, 102, 24)</string>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>40</x>
<y>170</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>Graph</string>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
<widget class="Line" name="line">
<property name="geometry">
<rect>
<x>0</x>
<y>120</y>
<width>200</width>
<height>20</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">color:rgb(255, 255, 255)</string>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="lineWidth">
<number>3</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</widget>
<widget class="QFrame" name="topbar">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>200</x>
<y>0</y>
<width>911</width>
<height>135</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>911</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>1920</width>
<height>16777215</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color:rgb(12, 76, 173)</string>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>430</x>
<y>70</y>
<width>113</width>
<height>20</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color:rgb(255, 255, 255)</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="placeholderText">
<string>Search</string>
</property>
</widget>
</widget>
<widget class="QFrame" name="body">
<property name="geometry">
<rect>
<x>200</x>
<y>130</y>
<width>911</width>
<height>591</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color:rgb(255, 254, 247)</string>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="lineWidth">
<number>0</number>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>
Upvotes: 1
Views: 1699
Reputation: 243887
As the docs points out:
loadUiType(uifile, from_imports=False, resource_suffix='_rc', import_from='.')
Load a Qt Designer .ui file and return a tuple of the generated form class and the Qt base class. These can then be used to create any number of instances of the user interface without having to parse the .ui file more than once.
(emphasis mine)
This returns 2 classes and will not fill any window, instead of using loadUiType you should use loadUi as pointed out by the docs:
loadUi(uifile, baseinstance=None, package='', resource_suffix='_rc')
Load a Qt Designer .ui file and returns an instance of the user interface.
(emphasis mine)
import os
import sys
from PyQt5 import QtWidgets, uic
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
ui_path = os.path.dirname(os.path.abspath(__file__))
uic.loadUi(os.path.join(ui_path, "myfile.ui"), self)
self.show()
def main():
app = QtWidgets.QApplication(sys.argv)
window = Ui()
app.exec_()
if __name__ == "__main__":
main()
Upvotes: 2