How to write Python code pyqt5 to attach 2 widgets on main window

Viewed 69

I have 3 ui files. I want to make 3 classes to load file MainWindow.ui and input widget1.ui and widget2.ui into MainWindow, and. So we just have one main window and in it 2 widgets that can communicate with each other. Also, an additional feature: how can we write a function in such a way that it listens to incoming data and then sends it away?

MainWindow

enter image description here

widget1

enter image description here

widget2

enter image description here

the result I want is

enter image description here

MainWindow.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>682</width>
    <height>391</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>280</x>
      <y>35</y>
      <width>131</width>
      <height>16</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>12</pointsize>
      <weight>75</weight>
      <bold>true</bold>
     </font>
    </property>
    <property name="text">
     <string>Main Window</string>
    </property>
   </widget>
   <widget class="QWidget" name="widget1" native="true">
    <property name="geometry">
     <rect>
      <x>25</x>
      <y>95</y>
      <width>286</width>
      <height>216</height>
     </rect>
    </property>
    <widget class="QLabel" name="label_2">
     <property name="geometry">
      <rect>
       <x>75</x>
       <y>60</y>
       <width>47</width>
       <height>13</height>
      </rect>
     </property>
     <property name="text">
      <string>Widget1</string>
     </property>
    </widget>
   </widget>
   <widget class="QWidget" name="widget" native="true">
    <property name="geometry">
     <rect>
      <x>365</x>
      <y>80</y>
      <width>251</width>
      <height>236</height>
     </rect>
    </property>
    <widget class="QLabel" name="label_3">
     <property name="geometry">
      <rect>
       <x>60</x>
       <y>80</y>
       <width>47</width>
       <height>13</height>
      </rect>
     </property>
     <property name="text">
      <string>Widget2</string>
     </property>
    </widget>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>682</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

widget1.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>170</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QWidget" name="formLayoutWidget">
   <property name="geometry">
    <rect>
     <x>4</x>
     <y>20</y>
     <width>391</width>
     <height>106</height>
    </rect>
   </property>
   <layout class="QFormLayout" name="formLayout">
    <item row="0" column="0">
     <widget class="QLabel" name="label">
      <property name="text">
       <string>Mychoice</string>
      </property>
     </widget>
    </item>
    <item row="0" column="1">
     <widget class="QComboBox" name="comboBox"/>
    </item>
   </layout>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

widget2.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>229</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QTableWidget" name="tableWidget">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>25</y>
     <width>326</width>
     <height>192</height>
    </rect>
   </property>
   <row>
    <property name="text">
     <string>apple</string>
    </property>
   </row>
   <row>
    <property name="text">
     <string>mango</string>
    </property>
   </row>
   <column>
    <property name="text">
     <string>New Column</string>
    </property>
   </column>
   <column>
    <property name="text">
     <string>number</string>
    </property>
   </column>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>
1 Answers

You can start by using the function PyQt5.uic.loadUi, then manipulate and combine your widgets in a python script. It'd look like this:

from PyQt5.uic import loadUi
from PyQt5 import QtCore, QtGui, QtWidgets
import sys

app = QtWidgets.QApplication(sys.argv)

m = loadUi('MainWindow.ui')
w1 = loadUi('widget1.ui')
w2 = loadUi('widget2.ui')

w = QtWidgets.QWidget()
m.setCentralWidget(w)

hbox = QtWidgets.QHBoxLayout()
w.setLayout(hbox)

hbox.addWidget(w1)
hbox.addWidget(w2)

m.show()
sys.exit(app.exec_())
Related