QtDesigner changes will be lost after redesign User Interface
The easiest way is to use the *.ui file directly in the python code, you don't need convert to *.py file every time you change the ui.
you can use this pseudo code in your project.
# imports
from PyQt5 import uic
# load ui file
baseUIClass, baseUIWidget = uic.loadUiType("MainGui.ui")
# use loaded ui file in the logic class
class Logic(baseUIWidget, baseUIClass):
def __init__(self, parent=None):
super(Logic, self).__init__(parent)
self.setupUi(self)
.
.
.
.
def main():
app = QtWidgets.QApplication(sys.argv)
ui = Logic(None)
ui.showMaximized()
sys.exit(app.exec_())
Qt Designer and Python run a function in separate functions.py
First, You should rename mainwindow
class to MainWindow
.MainWindow
need to be subclassed from QMainWindow
only.
After creating Ui_MainWindow
object, use its setupUi
method to build your MainWindow
object.
All the widgets reference that you created in designer will be available as attributes of Ui_MainWindow
object.
So, store it in main window as self.ui = Ui_MainWindow()
. Build main window as self.ui.setupUi(self)
. Reference any widgets like self.ui.contactsTable
You need to create object of your MainWindow
class, not QMainWindow
Try like this:
import sys
from PySide6.QtWidgets import QMainWindow, QApplication
from mainwindow import Ui_MainWindow
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Skills In Motion")
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.fillContactList()
def fillContactList(self):
# Reference Table like this
# self.ui.contactsTable
pass
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
How to get self.close to work, when using Qt designer?
Subclassing the "form class" (the one created from the ui) is pointless, as it's just a python class that does almost nothing on its own.
What you need is to implement the methods provided by the QWidget you're using (QMainWindow, in your case), so you need to inherit from both the widget and the form classes:
class DNAMainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
# ...
class DNAEngine:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.app = qtw.QApplication(sys.argv)
def setup(self):
self.mainWindow = DNAMainWindow()
def display(self):
self.mainWindow.show()
Using qtDesigner with python seamlessly
I found an even cleaner method for working with this, that does not require preemptive conversion after each edit at all. Instead it takes the .ui
file itself, so all you need to do is restart the program itself to update the design.
import sys
import os
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5 import uic
path = os.path.dirname(__file__) #uic paths from itself, not the active dir, so path needed
qtCreatorFile = "XXXXX.ui" #Ui file name, from QtDesigner, assumes in same folder as this .py
Ui_MainWindow, QtBaseClass = uic.loadUiType(path + qtCreatorFile) #process through pyuic
class MyApp(QMainWindow, Ui_MainWindow): #gui class
def __init__(self):
#The following sets up the gui via Qt
super(MyApp, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
#set up callbacks
self.ui.NAME OF CONTROL.ACTION.connect(self.test)
def test(self):
#Callback Function
if __name__ == "__main__":
app = QApplication(sys.argv) #instantiate a QtGui (holder for the app)
window = MyApp()
window.show()
sys.exit(app.exec_())
Note that this is Qt5. Qt5 and Qt4 are not API compatible, so it will be a little different in Qt4 (and presumably earlier as well).
TypError on MainWindow (self, str)
I suppose you're inheriting in some way what pyuic
generated code does, and I warmly suggest you to avoid to mimic their behavior.
As already suggested in the comments, you should modify the first two lines after the super()
:
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Gen")
self.setFixedSize(1250,802)
Why is that?
Both methods are instance methods, and all instance methods need that the first argument is an instance of the class.
Instance methods expect an instance reference as first argument (what is normally called self
). When calling the method directly from an instance, the instance argument (self
) is implicitly set.
Imagine this:
class MyClass(object):
def someMethod(self, argument=None):
print('self is "{}", argument is "{}"'.format(self, argument))
Then run the following:
>>> MyClass.someMethod('hello')
self is "hello", argument is "None"
>>> instance = MyClass()
>>> instance.someMethod('hello')
self is "<__main__.MyClass object at 0xb5c27dec>", argument is "hello"
As you can see, the first attempt shows that self
is the string argument you've given, while the second correctly shows that self
is an instance of MyClass and the argument is "hello". And that's exactly what has happened in your case: calling the function using the class instead of the instance has made your first string argument the instance argument.
(A very simplicistic) note about method types: PyQt is a binding, and works as a sort of layer between python and the actual Qt C++ objects. In this case, class methods are sometimes "unbound methods" before the class is instanciated while instance methods become "bound methods". The correct explanation is actually a bit more complex, but this is not the place to discuss this.
The UI designed by Qtdesigner is not the same as the one previewed in Qtdesigner when translated into Python code
change class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
to class MyApp(QtWidgets.QWidget, Ui_MainWindow):
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic
qtCreatorFile = "test22.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
#class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
class MyApp(QtWidgets.QWidget, Ui_MainWindow):
def __init__(self):
super().__init__()
Ui_MainWindow.__init__(self)
self.setupUi(self)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
Related Topics
Force Python to Use an Older Version of Module (Than What I Have Installed Now)
How to Update a Python Package
Extracting Text from HTML File Using Python
Python Pysftp Get_R from Linux Works Fine on Linux But Not on Windows
How to Retrieve the Values of Dynamic HTML Content Using Python
How to Embed HTML into Ipython Output
Pandas To_Html() Truncates String Contents
How to Activate Virtualenv in Linux
How to Disable CSS in Python Selenium Using Chromeoptions
What Do I Use on Linux to Make a Python Program Executable
MySQL_Config Not Found When Installing MySQLdb Python Interface
Linux Command-Line Call Not Returning What It Should from Os.System
How to Get a Cron Like Scheduler in Python