I've created a interface in Qt as .ui file and then converted it to a python file. Then, I wanted to add some functionality to the components such as radio button, etc. For doing so, I tried to re-implement the class from Qt and add my events. But it gives the following error:
self.radioButton_2.toggled.connect(self.radioButton2Clicked)
NameError: name 'self' is not defined
My first question is whether this is the correct/proper way to deal with classes generated by Qt? And second, why do I get the error?
My code is here:
import sys
from PySide import QtCore, QtGui
from InterfaceClass_Test01 import Ui_MainWindow
class MainInterface(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainInterface, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def setupUi(self, MainWindow):
super(MainInterface, self).setupUi(parent, MainWindow)
self.radioButton.toggled.connect(self.radioButtonClicked)
self.radioButton_2.toggled.connect(self.radioButton2Clicked)
self.radioButton_3.toggled.connect(self.radioButton3Clicked)
def radioButton3Clicked(self, enabled):
pass
def radioButton2Clicked(self, enabled):
pass
def radioButtonClicked(self, enabled):
pass
The generated files are a little unintuitive. The UI class is just a simple wrapper, and is not a sub-class of your top-level widget from Qt Designer (as you might expect).
Instead, the UI class has a setupUi method that takes an instance of your top-level class. This method will add all the widgets from Qt Designer and make them attributes of the passed in instance (which would normally be self). The attribute names are taken from the objectName property in Qt Designer. It is a good idea to reset the default names given by Qt to more readable ones so that they are easy to refer to later. (And don't forget to re-generate the UI module after you've made your changes!)
The module that imports the UI should end up looking like this:
import sys
from PySide import QtCore, QtGui
from InterfaceClass_Test01 import Ui_MainWindow
class MainInterface(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainInterface, self).__init__(parent)
# inherited from Ui_MainWindow
self.setupUi(self)
self.radioButton.toggled.connect(self.radioButtonClicked)
self.radioButton_2.toggled.connect(self.radioButton2Clicked)
self.radioButton_3.toggled.connect(self.radioButton3Clicked)
def radioButton3Clicked(self, enabled):
pass
def radioButton2Clicked(self, enabled):
pass
def radioButtonClicked(self, enabled):
pass
Related
I'm using Qt Designer for design GUI to use in python, after designing my desired UI in Qt Designer, convert it to python code and then I changed generated code to do some action in my python code, but if I changed the UI with Qt Designer and convert it to python code again, I lost my previous changes on my code.
how can I solve the problem?
can we Spreading a Class Over Multiple Files in python to write code in other files?
To avoid having these problems it is advisable not to modify this file but to create a new file where we implement a class that uses that design.
For example, suppose you have used the MainWindow template in the design.ui file, then convert it to Ui_Design.py like to the following structure:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
[...]
def retranslateUi(self, MainWindow):
[...]
Then we will create a new file that we will call logic.py where we will create the file that handles the logic and that uses the previous design:
class Logic(QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
So even if you modify the design and generate the file again .py you will not have to modify the file of the logic.
To generalize the idea we must have the following rules but for this the logic class must have the following structure:
class Logic(PyQtClass, DesignClass):
def __init__(self, *args, **kwargs):
PyQtClass.__init__(self, *args, **kwargs)
self.setupUi(self)
PyQtClass: This class depends on the design chosen.
Template PyQtClass
─────────────────────────────────────────────
Main Window QMainWindow
Widget QWidget
Dialog with Buttons Bottom QDialog
Dialog with Buttons Right QDialog
Dialog with Without Buttons QDialog
DesignClass: The name of the class that appears in your design.
The advantage of this implementation is that you can implement all the logic since it is a widget, for example we will implement the solution closing pyqt messageBox with closeevent of the parent window :
class Logic(QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
def closeEvent(self, event):
answer = QtWidgets.QMessageBox.question(
self,
'Are you sure you want to quit ?',
'Task is in progress !',
QtWidgets.QMessageBox.Yes,
QtWidgets.QMessageBox.No)
if answer == QtWidgets.QMessageBox.Yes:
event.accept()
else:
event.ignore()
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_())
I'm using Qt Designer for design GUI to use in python, after designing my desired UI in Qt Designer, convert it to python code and then I changed generated code to do some action in my python code, but if I changed the UI with Qt Designer and convert it to python code again, I lost my previous changes on my code.
how can I solve the problem?
can we Spreading a Class Over Multiple Files in python to write code in other files?
To avoid having these problems it is advisable not to modify this file but to create a new file where we implement a class that uses that design.
For example, suppose you have used the MainWindow template in the design.ui file, then convert it to Ui_Design.py like to the following structure:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
[...]
def retranslateUi(self, MainWindow):
[...]
Then we will create a new file that we will call logic.py where we will create the file that handles the logic and that uses the previous design:
class Logic(QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
So even if you modify the design and generate the file again .py you will not have to modify the file of the logic.
To generalize the idea we must have the following rules but for this the logic class must have the following structure:
class Logic(PyQtClass, DesignClass):
def __init__(self, *args, **kwargs):
PyQtClass.__init__(self, *args, **kwargs)
self.setupUi(self)
PyQtClass: This class depends on the design chosen.
Template PyQtClass
─────────────────────────────────────────────
Main Window QMainWindow
Widget QWidget
Dialog with Buttons Bottom QDialog
Dialog with Buttons Right QDialog
Dialog with Without Buttons QDialog
DesignClass: The name of the class that appears in your design.
The advantage of this implementation is that you can implement all the logic since it is a widget, for example we will implement the solution closing pyqt messageBox with closeevent of the parent window :
class Logic(QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
def closeEvent(self, event):
answer = QtWidgets.QMessageBox.question(
self,
'Are you sure you want to quit ?',
'Task is in progress !',
QtWidgets.QMessageBox.Yes,
QtWidgets.QMessageBox.No)
if answer == QtWidgets.QMessageBox.Yes:
event.accept()
else:
event.ignore()
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_())
I'm using Qt Designer for design GUI to use in python, after designing my desired UI in Qt Designer, convert it to python code and then I changed generated code to do some action in my python code, but if I changed the UI with Qt Designer and convert it to python code again, I lost my previous changes on my code.
how can I solve the problem?
can we Spreading a Class Over Multiple Files in python to write code in other files?
To avoid having these problems it is advisable not to modify this file but to create a new file where we implement a class that uses that design.
For example, suppose you have used the MainWindow template in the design.ui file, then convert it to Ui_Design.py like to the following structure:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
[...]
def retranslateUi(self, MainWindow):
[...]
Then we will create a new file that we will call logic.py where we will create the file that handles the logic and that uses the previous design:
class Logic(QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
So even if you modify the design and generate the file again .py you will not have to modify the file of the logic.
To generalize the idea we must have the following rules but for this the logic class must have the following structure:
class Logic(PyQtClass, DesignClass):
def __init__(self, *args, **kwargs):
PyQtClass.__init__(self, *args, **kwargs)
self.setupUi(self)
PyQtClass: This class depends on the design chosen.
Template PyQtClass
─────────────────────────────────────────────
Main Window QMainWindow
Widget QWidget
Dialog with Buttons Bottom QDialog
Dialog with Buttons Right QDialog
Dialog with Without Buttons QDialog
DesignClass: The name of the class that appears in your design.
The advantage of this implementation is that you can implement all the logic since it is a widget, for example we will implement the solution closing pyqt messageBox with closeevent of the parent window :
class Logic(QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
def closeEvent(self, event):
answer = QtWidgets.QMessageBox.question(
self,
'Are you sure you want to quit ?',
'Task is in progress !',
QtWidgets.QMessageBox.Yes,
QtWidgets.QMessageBox.No)
if answer == QtWidgets.QMessageBox.Yes:
event.accept()
else:
event.ignore()
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_())
I have a main window created in one python module. This module also contains a class definition used to create an object called my_settings.
I also have a separate module which creates a another window (which should be in a different module for various reasons). This window allows various inputs such as opening a file and storing some settings once buttons are clicked blaa blaa.
What I want to do is then amend the attributes of my_setttings. A simplified version of the code is:
MAIN MODULE
class MainWindow(QMainWindow):
def __init__(self,parent=None):
super(MainWindow, self).__init__(parent)
QMainWindow.__init__(self)
uic.loadUi(self.some_directory + "arc_custom_main.ui", self)
self.some_button.clicked.connect(self.open_some_widget)
def open_some_widget(self):
widget = widget_in_other_module(self)
widget_in_other_module.exec_()
app = QApplication(sys.argv)
my_settings=settings()
_mainWindow = MainWindow()
_mainWindow.show()
SEPERATE MODULE
class widget_in_other_module(QDialog):
def __init__(self, parent):
my_settings.temppath = my_settings.OutputDir
QDialog.__init__(self)
self.parent = parent
my_settings.some_attribute= foo
uic.loadUi("some.ui", self)
self.pushButtonOpenMain.clicked.connect(self.openMain)
def openMain(self):
my_settings.some_other_attibute=bar
The problem I have is that I can't find a way to be able to access my_settings in the openMain method of widget_in_other_module. I'm a bit of a newbe to qt and can't for the life of me work out where I should pass my_settings. For various reasons I need all the functionality of widget_in_other_module to be outside of the main module (mostly to do with readability and future planed changes to the main module). I've tried as much as I can think of e.g. including it as a parameter in the line self.pushButtonOpenMain.clicked.connect(self.openMain,my_settings) but this doesn't seem to be allowed. Am I missing something basic here?
Instantiate settings variable in separate module and import this varable where you need it. It could be object of a class or just dictionary.
storage.py
class Settings:
def __init__(self):
print("Settings")
my_settings = Settings()
MainWindow.py
from PyQt5.QtWidgets import QMainWindow, QApplication
import sys
from Widget import Widget
from storage import my_settings
class MainWindow(QMainWindow):
def __init__(self,parent=None):
super(MainWindow, self).__init__(parent)
QMainWindow.__init__(self)
self.open_some_widget()
def open_some_widget(self):
widget = Widget()
widget.show()
widget.test()
self._widget = widget
my_settings.foo = 10
app = QApplication(sys.argv)
_mainWindow = MainWindow()
_mainWindow.show()
app.exec_()
Widget.py
from PyQt5.QtWidgets import QWidget
from storage import my_settings
class Widget(QWidget):
def test(self):
print(my_settings.foo)
First of all let me tell you that I am new to Qt and also to Python.
I am using Qt(Taurusdesigner) to create my GUIs.
After starting Qt(Taurusdesigner), I generate my python code for that particular GUI using:
taurusuic4 -x -o file.py file.ui
or
pyuic4 -x -o file.py file.ui
After executing this command on command line I am able to generate python file, but the auto generated classes looks like:
class MainWindow(object):
def setupUi(self, MainWindow):
Where as when I am searching for any help on Google I find class written like:
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
What would I do to generate 2nd type of class file using Qt(Taurusdesigner)??
Why there is a syntax difference in my class and class which are written for help on internet.
Please Help regarding this.
Thanks in advance.
The ui module generated by taurusuic4/pyuic4 should be imported into your main application. You do not need to use the -x option, and obviously you should choose a better module name than "file":
taurusuic4 -o mainwindow.py file.ui
Your main application module should look something like this:
from PyQt4.QtGui import QMainWindow
from mainwindow import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.handleButton)
def handleButton(self):
print('Hello World!')
This approach means that all the widgets from Qt(Taurus) Designer end up as attributes of the MainWindow class. Another approach is to have the ui elements within a separate namespace:
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.handleButton)
setupUI and __init__ are two methods of the class MainWindow. For one class, there can be any number of method and you can put them in whatever order you like.
There is always an __init__ method, it's called a constructor. This method is called when you create an object (for example when you do myWindow=MainWindow()). It's usually put at the beginning because it'll be call first. Specifically for QT, you have to call the parent's constructor with super.
setupUI is the method created by your designer, to take care of layout and such. It should be called in the constructor.
Your code should look like:
class MainWindow(object):
def setupUi(self, MainWindow):
#code made by the designer
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
#some code
def another_method(self):
#some more code