PyQt5 button click not work in imported class
In file index.py this line works fine, but in imported class similar line won't work! I can't understand why. Here is code:
index.py
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QMainWindow, QApplication
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# Set up the MainWindow from Designer.
uic.loadUi("mw.ui", self)
# Connect up the buttons.
self.pushButton.clicked.connect(self.BtnClck)
self.show()
def BtnClck(self):
# Set up the ContentWindow from Designer.
from form1 import form1
form1(self.mn_general)
self.mn_general.pushButton_2.clicked.connect(form1 .BtnClck1) #this works fine
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
sys.exit(app.exec_())
form1.py
from PyQt5 import uic
class form1:
def __init__(self, obj):
super().__init__()
uic.loadUi("form1.ui", obj)
obj.pushButton.setText('TextChanged on init') #this works fine
obj.pushButton.clicked.connect(self.BtnClck1) #this NOT works
obj.pushButton.click() #this make previous line works fine!
def BtnClck1(self):
print('SecondWindowPrint')
Re: PyQt5 button click not work in imported class
What doesn't work about it?
Invalid syntax, error message printed at run time, no message when clicked...?
Please use [code]...[/code] tags around code.
Re: PyQt5 button click not work in imported class
Then i click pushButton by mice, it not work, no BtnClck1 method is called and no print-SecondWindowPrint.
But if i call PushButton click programmatically, it works fine.
And PushButton works fine if i make connect from index.py
Here is full code on GitHub github.com/m0x3/test1
Re: PyQt5 button click not work in imported class
Right answer is put instance of form1 class in a public variable in MainWindow class.
Need change BtnClck function in Index.py like this:
Code:
def BtnClck(self):
# Set up the ContentWindow from Designer.
from form1 import form1
self.Form=form1(self.mn_general)
self.mn_general.pushButton_2.clicked.connect(form1.BtnClck1) #this works fine
Really thanks to Saj Haz from StackOverflow