How to embed Kivy or Kivymd GUI into PyQt5 App

215 Views Asked by At

I have 2 basic apps (kivy & pyqt5) merged and separately they work just fine. I am trying to embed KIVY or KIVYmD GUI into a running PyQt5 App.

Full Code

import os, sys
from PyQt5 import QtCore, QtGui, QtQuick, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager,Screen
##from kivy.core.window import Window
##Window.size = (280, 500)

Kivy_Style = '''
FloatLayout:
    MDLabel:
        pos_hint:{'center_x':.9,'center_y':.85}
        text:'LOGIN'
        font_size:80
    MDTextField:
        id:user
        hint_text:'Username'
        pos_hint:{'center_x':.5,'center_y':.75}
        size_hint:.5,.1
    MDTextField:
        id:passw
        hint_text:'Password'
        password:True
        pos_hint:{'center_x':.5,'center_y':.65}
        size_hint:.5,.1
    MDRoundFlatButton:
        text:'Login'
        pos_hint:{'center_x':.5,'center_y':.55}
        size_hint:.2,.1
    
'''

class KivyApp(MDApp):
    def build(self):
        return Builder.load_string(Kivy_Style)            
# if __name__ == '__main__':
#     KivyApp().run()

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setWindowTitle("PyQt5 embed Kivy"); self.resize(300, 200)

        centralwgt  = QtWidgets.QWidget()
        lay = QtWidgets.QGridLayout(centralwgt)
        self.setCentralWidget(centralwgt)
        # CntKV_widget = QtWidgets.QWidget.createWindowContainer(centralwgt)
        try:
            lay.addWidget(KivyApp)   # error
        except Exception as _1srt_bug:
            print("_1srt_bug:", _1srt_bug)
            ## if you uncomment these 2lines below & comment the third, kivyapp run normal and when close we still sow 2separate GUI
            # kvGui = KivyApp
            # kvGui().run()
            # lay.addWidget(self, KivyApp().run) 
        else:
            CntKV_widget = QtWidgets.QWidget.createWindowContainer(KivyApp)
            lay.addWidget(CntKV_widget)
            # pass
        finally:
            pass

app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()

After many attempts:

I noticed, Kivy always run the Main windows even if i simply just import kivy modules modules; and when I add PyQt5 app and remove KivyApp().run(), 2 different/separate windows are running. At this point now i try to attach Kivy GUI into PyQt5 using QGridLay().addWidget() and QtWidgets.QWidget.createWindowContainer().

when i try to embed the kivy MDApp class i get the error below

TypeError: arguments did not match any overloaded call:
   addWidget(self, QWidget): argument 1 has unexpected type 'type'
   addWidget(self, QWidget, int, int, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment()): argument 1 has unexpected type 'type'
   addWidget(self, QWidget, int, int, int, int, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment()): argument 1 has unexpected type 'type'

enter image description here

0

There are 0 best solutions below