I tried to design a pyqt gui in when I press "x" will stop run function a counter , what should i do ?
I only know to use the following code
def closeEvent(self,event):
pass
Logic Code
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtCore import *
import sys
import time
from GUI import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.thread = MyThread(self)
self.thread.trigger.connect(self.textBrowser)
self.thread.start()
def textBrowser(self,a):
self.ui.textBrowser_4.clear()
self.ui.textBrowser_4.append(str(a))
def closeEvent(self,event):
pass
class MyThread(QThread):
trigger = pyqtSignal(str)
def run(self):
a = 0
while True:
a = a + 1
self.trigger.emit(str(a))
time.sleep(1)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
MainWindow = MainWindow()
MainWindow.show()
sys.exit(app.exec_())
GUI Code
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(944, 587)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(30, 310, 181, 41))
self.label_2.setObjectName("label_2")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(30, 20, 141, 21))
self.label_4.setObjectName("label_4")
self.textBrowser_4 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_4.setGeometry(QtCore.QRect(230, 300, 241, 61))
self.textBrowser_4.setObjectName("textBrowser_4")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_2.setText(_translate("MainWindow", "counter"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Add an initiator and an is_running variable in your Thread class like so :
def __init__(self):
self.is_running = True
Then make your loop depend on the truthness of this new variable :
def run(self):
a = 0
while self.is_running:
... your logic here
And finally when you want to exit in closeEvent() :
self.thread.is_running = False
Cheers
Related
I have recreated a problem I am encountering as a minimal example below.
The situation: I have two Qt Designer generated GUI, each being called by their own separated scripts. A third script is meant to collect information from the first script upon the click of a button on the second script. I does not, yet there is no errors.
I have also attempted to solve this by using signals, but these does not seem to communicate between scripts. I provided a simpler version here that doesn't use signals per se.
My question is: How do You get a third script to handle information of two other scripts related to GUIs in pyqt5 ?
Here is the minimal example:
The first GUI script:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(504, 223)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.TypeHere = QtWidgets.QTextEdit(self.centralwidget)
self.TypeHere.setObjectName("TypeHere")
self.verticalLayout.addWidget(self.TypeHere)
self.HelloButton = QtWidgets.QPushButton(self.centralwidget)
self.HelloButton.setObjectName("HelloButton")
self.verticalLayout.addWidget(self.HelloButton)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 504, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.HelloButton.setText(_translate("MainWindow", "Say hello"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
The second GUI script:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(282, 392)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.pushButton01 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton01.setObjectName("pushButton01")
self.verticalLayout.addWidget(self.pushButton01)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 282, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton01.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
JustSomeTextv01, the script of the first GUI:
from PyQt5 import QtWidgets
from PyQt5.QtCore import QProcess, QThreadPool
from TypingUIv01 import Ui_MainWindow
import JustSomeButtonsv01 as JSB
import sys
class Window(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setupUi(self)
self.HelloButton.pressed.connect(self.openButtons)
self.Display = JSB.Window()
self.ButtonsThread()
def openButtons(self):
self.Display.show()
def ButtonsThread(self):
self.threadpoolbutt = QThreadPool()
self.threadpoolbutt.start(self.runButtons)
def runButtons(self):
self.butt = QProcess()
print("Buttons Running")
self.butt.execute('python',['JustSomeButtonsv01.py'])
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
JustSomeButtonsv01, the script of the second GUI:
from PyQt5 import QtWidgets
from PyQt5.QtCore import QProcess, QThreadPool
from ButtonsUIv01 import Ui_MainWindow
# import JustSomeRecordv01 as JSR
import sys
class Window(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setupUi(self)
self.RecordThread()
def RecordThread(self):
self.threadpoolrec = QThreadPool()
self.threadpoolrec.start(self.runRecord)
def runRecord(self):
self.rec = QProcess()
print("Record Running")
self.rec.execute('python',['JustSomeRecordv01.py'])
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
# window.show()
sys.exit(app.exec())
And finally, JustSomeRecordv01, the third script trying to interact with the other two:
from PyQt5 import QtWidgets
import sys
from TypingUIv01 import Ui_MainWindow as JSTWin
from ButtonsUIv01 import Ui_MainWindow as ButtWin
class Record(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
# self.setupUi(self)
app2 = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
self.Win = JSTWin()
self.Win.setupUi(MainWindow)
self.Text = self.Win.TypeHere.toPlainText()
print("Running")
self.Butt = ButtWin()
self.Butt.setupUi(MainWindow)
self.Butt.pushButton01.pressed.connect(self.PrintIT)
def PrintIT(self):
print("Texting")
print(self.Text)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Record()
# window.show()
sys.exit(app.exec())
How to reproduce the problem:
You execute the JustSomeTextv01 script. Press the "Hello Button" and a second window will show up. You type anything in the QTextEdit of the first window and then click the button of the second window. The intent is that this second button would print what You wrote, but it doesn't work.
Thank You for your time,
I managed to do it within the example scripts, but the only solution was to put everything not-GUI into the same script as so:
# To test textbox related function
from PyQt5 import QtWidgets, QtCore
from TypingUIv01 import Ui_MainWindow
from ButtonsUIv01 import Ui_MainWindow as Ui2
import sys
class Window(QtWidgets.QMainWindow, Ui_MainWindow):
PatchSignal = QtCore.pyqtSignal()
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setupUi(self)
self.HelloButton.pressed.connect(self.openButtons)
self.PatchSignal.connect(self.printIT)
def openButtons(self):
self.w2 = Wintwo()
self.w2.show()
def printIT(self):
self.Text = self.TypeHere.toPlainText()
# print("PRINTING")
print(self.Text)
class Wintwo(QtWidgets.QMainWindow, Ui2):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setupUi(self)
self.pushButton01.pressed.connect(self.Emit)
self.emitter = window
def Emit(self):
# print("EMITTING")
self.emitter.PatchSignal.emit()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
It seems sending Signals between scripts is simply not possible in pyqt5!
Somehow this doesn't work in my actual script since "window" isn't recognized, but that's another question for another day.
I have a program that requires several scripts to work together.
I managed to recreate the situation with a simplified example.
I have a main window (here called the StartTestUIv01) that is launched from another script (here called LaunchTestv01) so that variables and options can be added in my actual script. This main window have buttons that allows the user to open another window (here the BasicGraphUIv01) that itself uses data obtained by another script (here the LaunchBasicGraphv01).
So the script chain becomes like this:
LaunchTestv01 -> StartTestUIv01 -> LaunchBasicGraphv01 -> BasicGraphUIv01
Here is the code for each:
LaunchTestv01
## This will launch StartTestUIv01 with the button
from PyQt5 import QtWidgets
from PyQt5.QtCore import QProcess
from StartTestUIv01 import Ui_MainWindow
class Window(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setupUi(self)
self.startGraph = QtWidgets.QAction('Launch Graph', self)
self.w = None #Set the default value
self.pushButton.clicked.connect(self.LaunchGraph)
def LaunchGraph(self):
if self.w is None:
print("Working")
# self.message("Executing Graph script")
self.w = QProcess()
self.w.finished.connect(self.closeGraph)
self.w.start("python",['LaunchBasicGraphv01.py'])
def closeGraph(self):
print("Closing")
# self.message("Graph script ended")
self.w = None
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
StartTestUIv01
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(313, 196)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(30, 20, 261, 121))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 313, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "START"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
LaunchBasicGraphv01
## This calculates values to insert in the Graph and displays that Window
from PyQt5 import QtWidgets
import BasicGraphUIv01 as ui
from pyqtgraph.Qt import QtCore
import numpy as np
x = np.arange(0,9)
y = np.random.randint(25, size=(9))
MainWindow = ui.QtWidgets.QMainWindow()
win = ui.Ui_MainWindow()
win.setupUi(MainWindow)
MainWindow.show()
# p0 = win.centralwidget.plot(x,y, pen='b')
p0 = win.graphWidget.plot(x,y, pen='y')
class Window(ui.QtWidgets.QMainWindow, ui.Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setupUi(self)
def update():
y = np.random.randint(25, size=(9))
p0.setData(x,y)
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(0)
BasicGraphUIv01
from PyQt5 import QtCore, QtGui, QtWidgets
from pyqtgraph import PlotWidget
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(473, 283)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.graphWidget = PlotWidget(self.centralwidget)
self.graphWidget.setGeometry(QtCore.QRect(10, 10, 451, 211))
self.graphWidget.setObjectName("graphWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 473, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Now the obvious problem is that the chain doesn't start !
It seems that if I'm launching a script that is itself launching another window, it just doesn't happen!
What is the proper way of doing this ?
EDIT: So I got banned because this solved question wasn't good enough or something.
To clarify; I was searching for the proper way of launching a PyQt script from another in the case where a python script is needed in-between to collect data.
Hope this helps.
Solved it!
Thanks to #musicamante for the clues.
I only had to change the two launching scripts as so:
LaunchTestv01
## This will launch StartTestUIv01 with the button
from PyQt5 import QtWidgets
from PyQt5.QtCore import QProcess
from StartTestUIv01 import Ui_MainWindow
class Window(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setupUi(self)
self.startGraph = QtWidgets.QAction('Launch Graph', self)
self.w = None #Set the default value
self.pushButton.clicked.connect(self.LaunchGraph)
def LaunchGraph(self):
if self.w is None:
print("Working")
# self.message("Executing Graph script")
self.w = QProcess()
self.w.finished.connect(self.closeGraph)
self.w.execute('python',['LaunchBasicGraphv01.py'])
def closeGraph(self):
print("Closing")
# self.message("Graph script ended")
self.w = None
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
LaunchBasicGraphv01
from PyQt5 import QtWidgets
from BasicGraphUIv01 import Ui_MainWindow
from pyqtgraph.Qt import QtCore
import numpy as np
import sys
app = QtWidgets.QApplication(sys.argv)
x = np.arange(0,9)
y = np.random.randint(25, size=(9))
MainWindow = QtWidgets.QMainWindow()
win = Ui_MainWindow()
win.setupUi(MainWindow)
MainWindow.show()
# p0 = win.centralwidget.plot(x,y, pen='b')
p0 = win.graphWidget.plot(x,y, pen='y')
class Window(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setupUi(self)
def update():
y = np.random.randint(25, size=(9))
p0.setData(x,y)
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(0)
if __name__ == '__main__':
sys.exit(app.exec())
Thanks for the help !
I have created a GUI with PyQt5. I have total three windows. first window has one push button (**btn_OpenSecondWIndow**) which opens a second window. second window has one push button (**btn_OpenCalendar**) which opens third (Calendar) window. user picks a date from there and select a button (**btn_selecteddate**) on third window, I want to display that selection in the label (label_Date) in the second window.
Snippet to get the flow
Now I'm at this point where the first window works just fine and the second window opens BUT pushbuttons doesn't work.
The button in the second window does nothing when the window is opened from the first one but when the class "SecondWindow" is called by its own and not from the first window, it works.
here is the code:
from PyQt5 import QtWidgets
from FirstWindow import Ui_FirstWindow
from SecondWindow import Ui_SecondWindow
from Calendar import Ui_CalendarWindow
class Calendar(QtWidgets.QMainWindow, Ui_CalendarWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
class FirstWindow(QtWidgets.QMainWindow, Ui_FirstWindow):
def __init__(self):
super(FirstWindow,self).__init__()
self.setupUi(self)
self.btn_OpenSecondWIndow.clicked.connect(self.open_SecondWindow)
def open_SecondWindow(self):
self.window = QtWidgets.QMainWindow()
self.ui = SecondWindow()
self.ui.setupUi(self.window)
self.window.show()
self.setEnabled(False)
self.window.setEnabled(True)
class SecondWindow(QtWidgets.QMainWindow, Ui_SecondWindow):
def __init__(self):
super(SecondWindow, self).__init__()
self.setupUi(self)
self.btn_OpenCalendar.clicked.connect(self.Open_Calendar)
def Open_Calendar(self):
self.window = Calendar()
self.window.setupUi(self.window)
self.window.show()
self.window.btn_Selecteddate.clicked.connect(self.PickedDate)
def PickedDate(self):
self.selecteddate = self.window.CalendarBox.selectedDate()
self.label_Date.setText(self.selecteddate.toString('MMM')+'-'+self.selecteddate.toString('yyyy'))
self.window.hide()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = FirstWindow()
window.show()
sys.exit(app.exec_())
Reference Ui Class codes are as below:
First Window:
from PyQt5 import QtCore, QtWidgets
class Ui_FirstWindow(object):
def setupUi(self, FirstWindow):
FirstWindow.setObjectName("FirstWindow")
FirstWindow.resize(380, 195)
self.centralwidget = QtWidgets.QWidget(FirstWindow)
self.centralwidget.setObjectName("centralwidget")
self.btn_OpenSecondWIndow = QtWidgets.QPushButton(self.centralwidget)
self.btn_OpenSecondWIndow.setGeometry(QtCore.QRect(80, 60, 221, 61))
self.btn_OpenSecondWIndow.setObjectName("btn_OpenSecondWIndow")
FirstWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(FirstWindow)
self.statusbar.setObjectName("statusbar")
FirstWindow.setStatusBar(self.statusbar)
self.retranslateUi(FirstWindow)
QtCore.QMetaObject.connectSlotsByName(FirstWindow)
def retranslateUi(self, FirstWindow):
_translate = QtCore.QCoreApplication.translate
FirstWindow.setWindowTitle(_translate("FirstWindow", "MainWindow"))
self.btn_OpenSecondWIndow.setText(_translate("FirstWindow", "Open Second Window"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
FirstWindow = QtWidgets.QMainWindow()
ui = Ui_FirstWindow()
ui.setupUi(FirstWindow)
FirstWindow.show()
sys.exit(app.exec_())
Second WIndow:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_SecondWindow(object):
def setupUi(self, SecondWindow):
SecondWindow.setObjectName("SecondWindow")
SecondWindow.resize(654, 242)
self.centralwidget = QtWidgets.QWidget(SecondWindow)
self.centralwidget.setObjectName("centralwidget")
self.label_Date = QtWidgets.QLabel(self.centralwidget)
self.label_Date.setGeometry(QtCore.QRect(330, 60, 281, 131))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.label_Date.setFont(font)
self.label_Date.setLayoutDirection(QtCore.Qt.LeftToRight)
self.label_Date.setObjectName("label_Date")
self.btn_OpenCalendar = QtWidgets.QPushButton(self.centralwidget)
self.btn_OpenCalendar.setGeometry(QtCore.QRect(80, 90, 191, 61))
self.btn_OpenCalendar.setObjectName("btn_OpenCalendar")
SecondWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(SecondWindow)
self.statusbar.setObjectName("statusbar")
SecondWindow.setStatusBar(self.statusbar)
self.retranslateUi(SecondWindow)
QtCore.QMetaObject.connectSlotsByName(SecondWindow)
def retranslateUi(self, SecondWindow):
_translate = QtCore.QCoreApplication.translate
SecondWindow.setWindowTitle(_translate("SecondWindow", "MainWindow"))
self.label_Date.setText(_translate("SecondWindow", "Date"))
self.btn_OpenCalendar.setText(_translate("SecondWindow", "Open Calendar"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
SecondWindow = QtWidgets.QMainWindow()
ui = Ui_SecondWindow()
ui.setupUi(SecondWindow)
SecondWindow.show()
sys.exit(app.exec_())
Third (Calendar) Window:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_CalendarWindow(object):
def setupUi(self, CalendarWindow):
CalendarWindow.setObjectName("CalendarWindow")
CalendarWindow.resize(512, 458)
self.centralwidget = QtWidgets.QWidget(CalendarWindow)
self.centralwidget.setObjectName("centralwidget")
self.CalendarBox = QtWidgets.QCalendarWidget(self.centralwidget)
self.CalendarBox.setGeometry(QtCore.QRect(20, 20, 464, 289))
self.CalendarBox.setObjectName("CalendarBox")
self.btn_Selecteddate = QtWidgets.QPushButton(self.centralwidget)
self.btn_Selecteddate.setGeometry(QtCore.QRect(160, 330, 181, 60))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.btn_Selecteddate.setFont(font)
self.btn_Selecteddate.setObjectName("btn_Selecteddate")
CalendarWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(CalendarWindow)
self.statusbar.setObjectName("statusbar")
CalendarWindow.setStatusBar(self.statusbar)
self.retranslateUi(CalendarWindow)
QtCore.QMetaObject.connectSlotsByName(CalendarWindow)
def retranslateUi(self, CalendarWindow):
_translate = QtCore.QCoreApplication.translate
CalendarWindow.setWindowTitle(_translate("CalendarWindow", "MainWindow"))
self.btn_Selecteddate.setText(_translate("CalendarWindow", "Select"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
CalendarWindow = QtWidgets.QMainWindow()
ui = Ui_CalendarWindow()
ui.setupUi(CalendarWindow)
CalendarWindow.show()
sys.exit(app.exec_())
Thanks in Advance. :)
In FirstWindow.open_SecondWindow you don't need to create a separate QMainWindow object for the second window. SecondWindow() already returns a QMainWindow object with the proper UI set up and a slot connected to btn_OpenCalendar for opening the calendar window. Therefore it is sufficient to do something like:
def open_SecondWindow(self):
self.window = SecondWindow()
self.window.show()
self.setEnabled(False)
self.window.setEnabled(True)
Im first trying to connect a sample cancel button that when clicked will show 'Hello'. Again, thats just a sample. I just like to know how can I connect my ui to the python code that id like to use for the cancel button. I hope someone can help me. Im new to python.
Heres the code:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import pyqtSlot
from A3AMainWindow import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__()
self.setupUi(self)
self.connect(self.btn_inv_cancel.clicked.connect(self.on_click))
#pyqtSlot()
def on_click(self):
QMessageBox.Information(Self, "hello")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Try it:
from PyQt5.QtWidgets import QMainWindow, QApplication, QMessageBox
from PyQt5.QtCore import pyqtSlot
from A3AMainWindow import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.on_click)
#pyqtSlot()
def on_click(self):
QMessageBox.information(self, "Announcement.","Hello")
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
A3AMainWindow.py
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(400, 300)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(134, 110, 121, 71))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 400, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Trying to proceed click button event in my code but got some issue.
AttributeError: 'MyMainWindow' object has no attribute 'pushButton'
Seems, like clicked event can`t find pushbutton from my subclass. Probably i did some mistakes in syntax, so really waiting for your help guys. Sorry if the question very banal, pyQt5 is simply new for me, trying to figure out in all of this.
Here is my files.
ui_main.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '1.ui'
#
# Created by: PyQt5 UI code generator 5.7.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(349, 131)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.formLayout = QtWidgets.QFormLayout()
self.formLayout.setObjectName("formLayout")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignTop)
self.lineEdit.setObjectName("lineEdit")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.SpanningRole, self.lineEdit)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.SpanningRole, self.label)
self.gridLayout.addLayout(self.formLayout, 0, 0, 1, 1)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
self.gridLayout.addLayout(self.horizontalLayout, 1, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 349, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "TextLabel"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
py_main.py
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, qApp
from PyQt5.QtCore import Qt, QEvent, QObject
from ui_main import Ui_MainWindow
class MyMainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyMainWindow, self).__init__(parent)
qApp.installEventFilter(self)
self.pushButton.clicked.connect(self.clickButton()) #here is where the issue is occurs
self.show()
self.setupUi(self)
def eventFilter(self, obj, event):
if event.type() == QEvent.KeyPress:
if event.key() == Qt.Key_Escape:
self.close()
return super(MyMainWindow, self).eventFilter(obj, event)
def clickButton(self):
sender = self.sender()
self.statusbar().showMessage(sender.text() + ' was pressed')
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MyMainWindow()
test = TestClass()
sys.exit(app.exec_())
Waiting forward for your help!
In your code there are 3 errors:
You must call the setupUI function first since the button is created here and then connect that button to the slot.
When the button is connected to the slot you just have to pass the slot name without parenthesis.
Change statusbar() to statusBar().
Code:
# ...
class MyMainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyMainWindow, self).__init__(parent)
qApp.installEventFilter(self)
self.setupUi(self)
self.pushButton.clicked.connect(self.clickButton)
self.show()
# ...
def clickButton(self):
sender = self.sender()
self.statusBar().showMessage(sender.text() + ' was pressed')
# ...
it work for me
call the setupUI function first
self.setupUi(self)
self.pushButton.clicked.connect(self.click_button)
tag QDialog and Ui_Dialog
main.py
MyMainWindow(QDialog, Ui_Dialog):
mainGui.py
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
OUTPUT:
SOLVED: AttributeError Main object has no attribute pushButton