How to make a function in one class implement another class? - python

I am creating a GUI with a code like this:
class MyClass2(QtGui.QMainWindow):
def __init__(self, win_parent = None):
def on_blahblah_clicked(x):
if __name__ == "__main__":
# Someone is launching this directly
# Create the QApplication
app = QtGui.QApplication(sys.argv)
#The Main window
main_window = HyperlinkWindow()
main_window.show()
# Enter the main loop
app.exec_()
And I have a code for an add-in button like this:
class MyClass(object):
"""Impementation of Some_addin.button (Button)"""
def __init__(self):
#Code here
def onClick(self):
# Code
pass
I want to create an add-in button that when clicked it brings out the GUI created above. So I want to be able to call the class of the GUI code, MyClass2 in function onClick. Just like I do with a in y in the code below:
class x:
a = 1 + 1
class y:
print x.a

Related

QtCore.signal not doing anything

I am a beginner with PyQt5 and I am having trouble to use QtCore.signal
I'd like to send a signal when I press my buttons and switch the current widget displayed.
I don't have any errors when I run my code but when I press the buttons nothing happen and I guess it is because I am doing something wrong with the QtCore.Signal
Here is my code :
from PySide2 import QtCore, QtWidgets
from ui_Page_accueil import Ui_MainWindow
from ui_NouvelleVerif import Ui_Dialog as Ui_NouvelleVerif
from ui_NouvelleVerifEssieux import Ui_Dialog as Ui_NouvelleVerifEssieux
import sys
class MainWindowUi(Ui_MainWindow):
to_NouvelleVerif = QtCore.Signal()
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.setupUi(self)
#self.pushButton.clicked.connect(self.pushbutton_handler1)
self.pushButton_2.clicked.connect(self.pushbutton_handler2)
#def pushbutton_handler1(self):
# self.to_MainWindow.emit()
def pushbutton_handler2(self):
self.to_NouvelleVerif.emit()
class NouvelleVerifUi(QtWidgets.QWidget, Ui_NouvelleVerif):
to_MainWindow = QtCore.Signal()
to_NouvelleVerifEssieux = QtCore.Signal()
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.pushbutton_handler1)
#self.pushButton_2.clicked.connect(self.pushbutton_handler2)
self.pushButton_3.clicked.connect(self.pushbutton_handler3)
def pushbutton_handler1(self):
self.to_MainWindow.emit()
#def pushbutton_handler2(self):
# self.switch_window.emit()
def pushbutton_handler3(self):
self.to_NouvelleVerifEssieux.emit()
class NouvelleVerifEssieuxUi(QtWidgets.QWidget, Ui_NouvelleVerifEssieux):
to_NouvelleVerif = QtCore.Signal()
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.pushbutton_handler1)
def pushbutton_handler1(self):
self.to_NouvelleVerif.emit()
class Controller :
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QStackedWidget()
MainWindow = MainWindowUi()
NouvelleVerif = NouvelleVerifUi()
NouvelleVerifEssieux = NouvelleVerifEssieuxUi()
def __init__(self):
self.widget.addWidget(self.MainWindow) # create an instance of the first page class and add it to stackedwidget
self.widget.addWidget(self.NouvelleVerif) # adding second page
self.widget.addWidget(self.NouvelleVerifEssieux)
self.widget.setCurrentWidget(self.MainWindow) # setting the page that you want to load when application starts up. you can also use setCurrentIndex(int)
def show_MainWindow(self):
self.NouvelleVerif = NouvelleVerifUi()
self.NouvelleVerif.to_MainWindow.connect(self.show_MainWindow)
self.widget.setCurrentWidget(self.MainWindow)
def show_NouvelleVerif(self):
self.MainWindow = MainWindowUi()
self.NouvelleVerifEssieux = NouvelleVerifEssieuxUi()
self.MainWindow.to_NouvelleVerif.connect(self.show_NouvelleVerif)
self.NouvelleVerifEssieux.to_NouvelleVerif.connect(self.show_NouvelleVerif)
self.widget.setCurrentWidget(self.NouvelleVerif)
def show_NouvelleVerifEssieux(self):
self.NouvelleVerif = NouvelleVerifUi()
self.NouvelleVerif.to_NouvelleVerifEssieux.connect(self.show_NouvelleVerifEssieux)
self.widget.setCurrentWidget(self.NouvelleVerifEssieux)
def main():
controller = Controller()
controller.widget.show()
sys.exit(controller.app.exec_())
if __name__ == '__main__':
main()
#musicamante I don't know why I though the QtCore.Signal could be call even if he was in a function which has not been called. I did what you said and I realise I made another mistake with the widgets.
In the controller class they are create and add to the QStackedWidget in the __init__, but I was creating new ones and trying to set them as CurrentWidget without adding them to the QStackedWidget.
#alexpdev I wanted to navigate through my three differents with pushButton UI this way :
Start with MainWindowUI
MainWindowUI pushButton_2.clicked --> set the current widget display to NouvelleVerifUI
NouvelleVerifUI pushButton_1.clicked --> set the current widget display back to MainWindowUI
NouvelleVerifUI pushButton_3.clicked --> set the current widget display to NouvelleVerifEssieuxUI
NouvelleVerifEssieuxUI pushButton_1.clicked --> set the current widget display back to NouvelleVerifUI
Now everything work I did what you said #musicamante and I also use the UI created at first.
class Controller :
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QStackedWidget()
MainWindow = MainWindowUi()
NouvelleVerif = NouvelleVerifUi()
NouvelleVerifEssieux = NouvelleVerifEssieuxUi()
def __init__(self):
self.widget.addWidget(self.MainWindow) # create an instance of the first page class and add it to stackedwidget
self.widget.addWidget(self.NouvelleVerif) # adding second page
self.widget.addWidget(self.NouvelleVerifEssieux)
self.widget.setCurrentWidget(self.MainWindow) # setting the page that you want to load when application starts up. you can also use setCurrentIndex(int)
self.NouvelleVerif.to_MainWindow.connect(self.show_MainWindow)
self.MainWindow.to_NouvelleVerif.connect(self.show_NouvelleVerif)
self.NouvelleVerifEssieux.to_NouvelleVerif.connect(self.show_NouvelleVerif)
self.NouvelleVerif.to_NouvelleVerifEssieux.connect(self.show_NouvelleVerifEssieux)
def show_MainWindow(self):
self.widget.setCurrentWidget(self.MainWindow)
def show_NouvelleVerif(self):
self.widget.setCurrentWidget(self.NouvelleVerif)
def show_NouvelleVerifEssieux(self):
self.widget.setCurrentWidget(self.NouvelleVerifEssieux)
Thank you all for your time

I want to know how to use pyqt5 to load the created widget at once

Sorry. I will modify the contents. I would like to load a widget inside def test by pressing Qbutton. Can not you use QStackedWidget to load the widget's configured functions? I've compiled the class and called it, but only a = QLineEdit ('Qline', self). I wonder what should be done to switch widgets.
You can also create a table like html using pyqt5.
import sys
from PyQt5.QtWidgets import *
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.stacked = QStackedWidget(self)
self.FirstpUI()
def FirstpUI(self):
self.btn1 = QPushButton('test1', self)
self.btn1.move(50,50)
self.btn1.clicked.connect(self.btn1_click)
def test(self):
a = QLineEdit('Qline', self)
b = QLineEdit('Qline2', self)
c = QPushButton('button', self)
a.move(0, 0)
b.move(100, 0)
c.move(50,50)
c.clicked.connect(self.btn2_click)
def btn1_click(self):
self.btn1.deleteLater()
self.stacked.addWidget(self.test())
self.stacked.setCurrentIndex(self.stacked.currentIndex()+1)
def btn2_click(self):
QMessageBox.about(self,'hello','hello2')
if __name__ == "__main__":
app = QApplication(sys.argv)
fream = MainWindow()
fream.show()
app.exec_()
May be I don't know what you real want,because I know that little, I think You can use QtDesigner,it's very useful

PyQt4: central widget and parent modules

My question is similar to this one, except in one thing: what if classes widget1 and widget2 are big and I want to put them in two separate files. Problem gets in method Check(). So, is there a way to call self.parent().setCentralWidget(w2) even though widget2 isn't in the same file as widget1 (but they are both imported in main file containing MainWindow).
Code taken from How to set the central widget of existing MainWidnow in Python PyQt4?
class widget1(QtGui.QFrame):
def __init__(self,parent = None):
......
......
def Check(self):
if (condition):
w2=widget2(self)
self.parent().setCentralWidget(w2)
class widget2(QtGui.QFrame):
def __int__(self,parent = None):
.....
.....
class MainWindow(QtGui.QMainWindow):
def __init__(self,parent = None):
QtGui.QMainWindow.__init__(self,parent)
....
mywidgetone = widget1()
self.setCentralWidget(mywidgetone)
if __name__ == '__main__':
app = QtGui.QApplicaiton(sys.argv)
main = MainWindow()
main.show()
app.exec_()

I need help making a menu bar in PyQt5

I have been trying to implement a menu bar in my program for a few days now and i cant seem to get one running. I would like someone to look at my code and give me a template to follow to making a menu bar.
class MainWindow(QMainWindow):
def __init__(self, databaseFilePath, userFilePath):
super(MainWindow,self).__init__()
self.moviesFilePath = moviesFilePath
self.currentUserFilePath = currentUserFilePath
self.createWindow()
def changeFilePath(self):
self.currentUserFilePath = functions_classes.changeFP()
functions_classes.storeFP(self.currentUserFilePath, 1)
def createWindow(self):
self.setWindowTitle('Movies')
#Menu Bar
fileMenuBar = QMenuBar().addMenu('File')
The method changeFilePath is what I would like to be called when a menu option called 'Change user database location' is called from the menu bar File. I have read that actions are the key to this but when every i have tried to implement them they haven't worked.
The QMainWindow class already has a menu-bar.
So you just need to add a menu to it, and then add an action to that menu, like this:
def createUI(self):
...
menu = self.menuBar().addMenu('File')
action = menu.addAction('Change File Path')
action.triggered.connect(self.changeFilePath)
EDIT:
Here's a full, working example based on your example class:
from PyQt5 import QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, databaseFilePath, userFilePath):
super(MainWindow,self).__init__()
self.databaseFilePath = databaseFilePath
self.userFilePath = userFilePath
self.createUI()
def changeFilePath(self):
print('changeFilePath')
# self.userFilePath = functions_classes.changeFilePath()
# functions_classes.storeFilePath(self.userFilePath, 1)
def createUI(self):
self.setWindowTitle('Equipment Manager 0.3')
menu = self.menuBar().addMenu('File')
action = menu.addAction('Change File Path')
action.triggered.connect(self.changeFilePath)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainWindow('some/path', 'some/other/path')
window.show()
window.setGeometry(500, 300, 300, 300)
sys.exit(app.exec_())
The logic to add a Menubar with usable items is something like this
def createUI(self):
self.setWindowTitle('Equipment Manager 0.3')
#Menu Bar
fileMenuBar = QMenuBar(self)
menuFile = QMenu(fileMenuBar)
actionChangePath = QAction(tr("Change Path"), self)
fileMenuBar.addMenu(menuFile)
menuFile.addAction(actionChangePath)
Then you just need to connect the action actionChangePath to the signal triggered() with something like
connect(actionChangePath,SIGNAL("triggered()"), changeFilePath)
Probably there are some better solution (but why did you not use the Designer ?), but this one should be work

How to call a function from a different class in Python

I have the following outline of a Python program (using PyQt4):
class Polygon( QtGui.QGraphicsItem ):
def __init__(self):
super(Polygon, self).__init__()
def addpoint( self, point ):
if last_point:
# trying to add auto-save here
class MainWidget(QtGui.QWidget):
openFileName = ""
list_of_polygons = []
def __init__(self):
super(MainWidget, self).__init__()
def openFile( self ):
call dialog
self.openFileName = ...
def saveFile( self ):
# needs to access a couple something with self.variables, like self.openFileName
def main():
app = QtGui.QApplication(sys.argv)
ex = MainWidget()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Functionality is an image viewer where I'm creating polygons to tag object. I'd like to call an auto-save once a polygon has been created.
So for saving a polygon I need to call it from the saveFile function MainWidget class. My problem is that the saving functionality is implemented in the MainWidget class, and I don't know how to access them from inside the Polygon class.
What would be the best idea to do this? Should I make saveFile global? If yes, then how do I access the self. variables of the MainWidget?
You probably need to pass the widget object to the Polygon when you create it so that it knows what its "parent" widget object is:
class Polygon( QtGui.QGraphicsItem ):
def __init__(self, parent):
self.parent = parent
# ...
(and then foo = Polygon(your_widget) when you create it).
Then you can just call self.parent.saveFile(...) or whatnot.

Categories