I want to call a function which uses an ui object created by Qt Designer from a separate file and I cannot seem to be able to figure out how to do that. This is to help me on a larger project I'm working on.
As of now I am getting the error
AttributeError: type object 'QMainWindow' has no attribute 'label'
My first file - main.py
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget
from PyQt5.QtCore import Qt
from PyQt5.uic import loadUi
class MainPage(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
loadUi("layout.ui",self)
from display_label_func import DISPLAY_LABEL
self.setWindowTitle("Example")
self.pushButton.clicked.connect(DISPLAY_LABEL.display_words)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
win = MainPage()
win.show()
sys.exit(app.exec_())
My 2nd file - display_label_func
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import*
class DISPLAY_LABEL(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
def display_words(self):
QMainWindow.label.setText('1.E4 is best by test')
My ui file - layout.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>595</width>
<height>452</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QWidget" name="widget" native="true">
<property name="styleSheet">
<string notr="true">background-color: rgb(33, 33, 33);</string>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>110</x>
<y>80</y>
<width>381</width>
<height>161</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>20</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 255, 255);
color: rgb(255, 0, 0);</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</widget>
</item>
<item row="1" column="0">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Button</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>595</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
How would I go about resolving this and get it to work?
Also, if I define and set a variable in the display_label_func file
could I use that variable in subsequent files all part of the same gui?
Thanks.