I am trying to go from window to window and back and forward.
I have tried using while True: and cannot seem to get it to work. It just prints HOME a million times and crashes the window.
from PyQt5 import QtCore, QtGui, QtWidgets
import IMAGE_rc
import sys
from GUIWINDOW1 import *
import time
class Ui_MainWindow(object):
def setup(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(800, 480)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
MainWindow.setMinimumSize(QtCore.QSize(800, 480))
MainWindow.setMaximumSize(QtCore.QSize(800, 480))
MainWindow.setMouseTracking(False)
MainWindow.setStyleSheet("background-color: rgb(170, 255, 127);")
MainWindow.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
MainWindow.setTabShape(QtWidgets.QTabWidget.Rounded)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setMinimumSize(QtCore.QSize(800, 480))
self.centralwidget.setMaximumSize(QtCore.QSize(800, 480))
self.centralwidget.setObjectName("centralwidget")
self.TOUCH1 = QtWidgets.QPushButton(self.centralwidget)
self.TOUCH1.setGeometry(QtCore.QRect(70, 10, 662, 200))
self.TOUCH1.setStyleSheet("border-image: url(/PROGRAMMING/QT DESIGNER/GUIIMAGES/LOGO.png);")
self.TOUCH1.setText("")
self.TOUCH1.setObjectName("TOUCH1")
self.TOUCH2 = QtWidgets.QPushButton(self.centralwidget)
self.TOUCH2.setGeometry(QtCore.QRect(210, 220, 360, 200))
self.TOUCH2.setStyleSheet("border-image: url(/PROGRAMMING/QT DESIGNER/GUIIMAGES/1200px-Florida_Atlantic_Owls_logo.svg.png);\n"
"background-color: rgb(170, 255, 127);")
self.TOUCH2.setText("")
self.TOUCH2.setObjectName("TOUCH2")
self.TOUCH4 = QtWidgets.QPushButton(self.centralwidget)
self.TOUCH4.setGeometry(QtCore.QRect(0, 0, 801, 481))
self.TOUCH4.setStyleSheet("")
self.TOUCH4.setObjectName("TOUCH4")
self.TOUCH3 = QtWidgets.QPushButton(self.centralwidget)
self.TOUCH3.setGeometry(QtCore.QRect(70, 430, 611, 41))
font = QtGui.QFont()
font.setPointSize(16)
self.TOUCH3.setFont(font)
self.TOUCH3.setStyleSheet("")
self.TOUCH3.setObjectName("TOUCH3")
self.TOUCH4.raise_()
self.TOUCH1.raise_()
self.TOUCH2.raise_()
self.TOUCH3.raise_()
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.TOUCH1.clicked.connect(self.BEGIN)
self.TOUCH2.clicked.connect(self.BEGIN)
self.TOUCH3.clicked.connect(self.BEGIN)
self.TOUCH4.clicked.connect(self.BEGIN)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.TOUCH4.setText(_translate("MainWindow", "PushButton"))
self.TOUCH3.setText(_translate("MainWindow", "TAP ANYWHERE TO BEGIN"))
def BEGIN(self):
print("HOME")
self.WINDOW1=QtWidgets.QMainWindow()
self.ui = Ui_WINDOW1()
self.ui.setup(self.WINDOW1)
self.WINDOW1.showFullScreen()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setup(MainWindow)
MainWindow.showFullScreen()
sys.exit(app.exec_())
Window closes when i go back and forward from MainWindow to WINDOW1 and try to go back to WINDOW1
Related
Below are the 2 window codes, 1st is main window and 2nd is widget window:
from Open import Ui_Form
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def open(self):
self.window = QtWidgets.QWidget()
self.ui = Ui_Form()
self.ui.setupUi(self.window)
self.window.show()
def get_result(self):
answer = []
Unsqueeze_result = set(answer)
final = list(Unsqueeze_result)
final.sort()
self.final_list = [x.upper() for x in final]
self.listView.addItems(self.final_list)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.listView = QtWidgets.QListView(self.centralwidget)
self.listView.setGeometry(QtCore.QRect(270, 140, 256, 371))
self.listView.setObjectName("listView")
self.result_button = QtWidgets.QPushButton(self.centralwidget)
self.result_button.setGeometry(QtCore.QRect(350, 70, 91, 41))
self.result_button.setObjectName("result_button")
self.result_button.pressed.connect(self.get_result)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionOpen = QtWidgets.QAction(MainWindow)
self.actionOpen.setObjectName("actionOpen")
self.actionOpen.triggered.connect(lambda:self.open())
self.menuFile.addAction(self.actionOpen)
self.menubar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.result_button.setText(_translate("MainWindow", "Result"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionOpen.setText(_translate("MainWindow", "Open"))
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_())
Below are 2nd window:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def __init__(self):
self.list_result = []
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 300)
self.checkBox_1 = QtWidgets.QCheckBox(Form)
self.checkBox_1.setGeometry(QtCore.QRect(60, 70, 121, 31))
self.checkBox_1.setObjectName("checkBox_1")
self.checkBox_2 = QtWidgets.QCheckBox(Form)
self.checkBox_2.setGeometry(QtCore.QRect(60, 110, 111, 31))
self.checkBox_2.setObjectName("checkBox_2")
self.pushButton_ok = QtWidgets.QPushButton(Form)
self.pushButton_ok.setGeometry(QtCore.QRect(90, 200, 71, 31))
self.pushButton_ok.setObjectName("pushButton")
self.pushButton_ok.pressed.connect(self.OK_thread)
self.pushButton_cancel = QtWidgets.QPushButton(Form)
self.pushButton_cancel.setGeometry(QtCore.QRect(210, 200, 71, 31))
self.pushButton_cancel.setObjectName("pushButton_2")
self.pushButton_cancel.clicked.connect(Form.close)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.checkBox_1.setText(_translate("Form", "Test1"))
self.checkBox_2.setText(_translate("Form", "Test 2"))
self.pushButton.setText(_translate("Form", "OK"))
self.pushButton_2.setText(_translate("Form", "Cancel"))
def OK_thread(self):
global submit_execute_thread
self.pushButton_ok.setEnabled(False)
self.pushButton_cancel.setEnabled(False)
submit_execute_thread = QThread()
submit_execute_thread.started.connect(self.ok_execute)
submit_execute_thread.start()
self.check_execute_thread()
def check_execute_thread(self):
if submit_execute_thread.isRunning() == True:
time.sleep(10)
self.check_execute_thread()
else:
Form.close()
def ok_execute(self):
if self.checkBox_1.isChecked() and self.checkBox_2.ischecked():
self.list_result = ["a","b","c","d","e"]
i have 2 issue here:
My objective is click the File --> Open and 2nd window appear, when i tick the checkbox1 and checkbox2 and press ok_button, i connect the ok_button to OK_thread and make a Qthread to ok_execute function to prevent freezing while running the function and keep checking the thread every 10 seconds until it run finish and close the Form window. I am not sure what is the issue here as it could not work, my GUI still freezing after i press OK_button and never run finish.
passing value:
i try to define the list = [] in def init() in both main window and 2nd window but some variable become not declarable such as "Form", so when i open the Form window from main window, it could not work. and another issue is how do i pass the value from Form window which is from ok_execute return self.list_result to Mainwindow, so when i click the result button in Main Window, it will show the list_result in the listview widget.
Please help me modify the code and appreciate your help.
Thank you.
Regards,
cstung89
I want to print some text and image designed by qtdesigner.
How could I change font and another format in QTextDocument?
from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport
class Ui_MainWindow(object):
def handlePrint(self):
dialog = QtPrintSupport.QPrintDialog()
if dialog.exec_() == QtWidgets.QDialog.Accepted:
self.handlePaintRequest(dialog.printer())
def handlePreview(self):
dialog = QtPrintSupport.QPrintPreviewDialog()
dialog.paintRequested.connect(self.handlePaintRequest)
dialog.exec_()
def handlePaintRequest(self, printer):
document = QtGui.QTextDocument()
cursor = QtGui.QTextCursor(document)
cursor.insertImage('logo4.jpg')
cursor.insertBlock()
cursor.insertText('MY TEXT')
document.print_(printer)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton_print_previw = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_print_previw.setGeometry(QtCore.QRect(90, 270, 251, 91))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton_print_previw.setFont(font)
self.pushButton_print_previw.setObjectName("pushButton_print_previw")
self.pushButton_print = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_print.setGeometry(QtCore.QRect(440, 270, 251, 91))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton_print.setFont(font)
self.pushButton_print.setObjectName("pushButton_print")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
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)
############################################################
self.pushButton_print_previw.clicked.connect(self.handlePreview)
############################################################
############################################################
self.pushButton_print.clicked.connect(self.handlePrint)
############################################################
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_print_previw.setText(_translate("MainWindow", "Print Preview"))
self.pushButton_print.setText(_translate("MainWindow", "Print"))
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_())
document.print_(printer) is very convenient but it is not a little flexible.
Because you don't use QTextEdit, you must edit the format information by your code only. (except for setHtml in advance.)
If you draw QPrinter document as you like, you can use QPainter.
Normarly, although QPainter is used in paintEvent(self, event), only if you set a subclass of QPaintDevice to constructor of QPainter, you can draw as you like.
But you must calculate the allocation exactly.
QPrinter is a subclass of QPaintDevice.
Trap
The topleft of QPainter.drawText is the bottom line of Text.
(x, y)→ MY TEXT
Here is the code.
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport
class Ui_MainWindow(object):
def handlePrint(self):
dialog = QtPrintSupport.QPrintDialog()
if dialog.exec_() == QtWidgets.QDialog.Accepted:
self.handlePaintRequest(dialog.printer())
def handlePreview(self):
dialog = QtPrintSupport.QPrintPreviewDialog()
dialog.paintRequested.connect(self.handlePaintRequest)
dialog.exec_()
def handlePaintRequest(self, printer):
document = QtGui.QTextDocument()
# cursor = QtGui.QTextCursor(document)
# cursor.insertImage('logo4.jpg')
# cursor.insertBlock()
# cursor.insertText('MY TEXT')
# document.print_(printer)
documentRootFrameFormat = document.rootFrame().frameFormat()
documentLeftMargin = documentRootFrameFormat.leftMargin()
documentTopMargin = documentRootFrameFormat.topMargin()
drawn_im = QtGui.QImage("logo4.jpg")
drawn_im_height = drawn_im.height()
painter = QtGui.QPainter(printer)
painter.drawImage(documentLeftMargin, documentTopMargin, drawn_im)
painter_font = painter.font()
#you can set flag which font is used.
custom_font = QtGui.QFont("Segoe UI Light", 18)
fontMetricsF = QtGui.QFontMetricsF(custom_font)
painter.save()
painter.setFont(custom_font)
height_fontMetricsF = fontMetricsF.height()
painter.drawText(documentLeftMargin, documentTopMargin + drawn_im_height + height_fontMetricsF, 'MY TEXT')
painter.restore()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton_print_previw = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_print_previw.setGeometry(QtCore.QRect(90, 270, 251, 91))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton_print_previw.setFont(font)
self.pushButton_print_previw.setObjectName("pushButton_print_previw")
self.pushButton_print = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_print.setGeometry(QtCore.QRect(440, 270, 251, 91))
font = QtGui.QFont()
font.setPointSize(15)
self.pushButton_print.setFont(font)
self.pushButton_print.setObjectName("pushButton_print")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
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)
############################################################
self.pushButton_print_previw.clicked.connect(self.handlePreview)
############################################################
############################################################
self.pushButton_print.clicked.connect(self.handlePrint)
############################################################
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton_print_previw.setText(_translate("MainWindow", "Print Preview"))
self.pushButton_print.setText(_translate("MainWindow", "Print"))
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_())
My Result
If you want to use QTextCursor, you can set Font & format by using the method of QTextCursor.
You can get the similar result by this code.
I recommend this if you don't hope high control.
Other Solution
def handlePaintRequest(self, printer):
document = QtGui.QTextDocument()
cursor = QtGui.QTextCursor(document)
cursor.insertImage("logo4.jpg")
cursor.insertBlock()
charFormat = cursor.charFormat()
charFormat.setFont(QtGui.QFont("Segoe UI Light", 18))
cursor.insertText('MY TEXT', charFormat)
document.print_(printer)
I have a question in pyqt5. I want to make a Videoplayer. now I'm stuck that how can I gave them complete path it not to click on the button and go to path and select the video I just want that it this one file path. Please have a look to my code. I making this for both raspberry pi and window
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_VideoWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(733, 505)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.Video_Widget = QVideoWidget(self.centralwidget)
self.Video_Widget.setGeometry(QtCore.QRect(0, 0, 711, 361))
self.Video_Widget.setObjectName("Video_Widget")
self.horizontalScrollBar = QtWidgets.QScrollBar(self.centralwidget)
self.horizontalScrollBar.setGeometry(QtCore.QRect(0, 380, 721, 16))
self.horizontalScrollBar.setOrientation(QtCore.Qt.Horizontal)
self.horizontalScrollBar.setObjectName("horizontalScrollBar")
self.play = QtWidgets.QPushButton(self.centralwidget)
self.play.setGeometry(QtCore.QRect(10, 410, 75, 23))
self.play.setObjectName("play")
self.select = QtWidgets.QPushButton(self.centralwidget)
self.select.setGeometry(QtCore.QRect(550, 410, 75, 23))
self.select.setObjectName("select")
self.play.clicked.connect(self.load_video)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def load_video(self):
filepath = 'F:/Fun/Songs/12.mp4'
self.Video_Widget.load(filepath)
self.horizontalScrollBar.setMediaObject(self.Video_Widget.mediaObject())
self.Video_Widget.play()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.play.setText(_translate("MainWindow", "play"))
self.select.setText(_translate("MainWindow", "Select"))
from PyQt5.QtMultimediaWidgets import QVideoWidget
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_VideoWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I used this code too which is present in official documentation.
def load_video(self):
player = QMediaPlayer()
playlist = QMediaPlaylist(player)
playlist.addMedia(QUrl('F:/Fun/Songs/12.mp4'))
player.setVideoOutput(self.Video_Widget)
self.Video_Widget.show()
playlist.setCurrentIndex(1)
player.play()
while using this code which is in official documentation. this error occurred.
As the error and the docs point out, a QMediaContent must be passed to the addMedia method. To build a QMediaContent you can use a QUrl:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_VideoWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(733, 505)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.Video_Widget = QVideoWidget(self.centralwidget)
self.Video_Widget.setGeometry(QtCore.QRect(0, 0, 711, 361))
self.Video_Widget.setObjectName("Video_Widget")
self.horizontalScrollBar = QtWidgets.QScrollBar(self.centralwidget)
self.horizontalScrollBar.setGeometry(QtCore.QRect(0, 380, 721, 16))
self.horizontalScrollBar.setOrientation(QtCore.Qt.Horizontal)
self.horizontalScrollBar.setObjectName("horizontalScrollBar")
self.play = QtWidgets.QPushButton(self.centralwidget)
self.play.setGeometry(QtCore.QRect(10, 410, 75, 23))
self.play.setObjectName("play")
self.select = QtWidgets.QPushButton(self.centralwidget)
self.select.setGeometry(QtCore.QRect(550, 410, 75, 23))
self.select.setObjectName("select")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.play.setText(_translate("MainWindow", "play"))
self.select.setText(_translate("MainWindow", "Select"))
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5 import QtMultimedia
class VideoWindow(QtWidgets.QMainWindow, Ui_VideoWindow):
def __init__(self, parent=None):
super(VideoWindow, self).__init__(parent)
self.setupUi(self)
self.player = QtMultimedia.QMediaPlayer(self)
self.player.setVideoOutput(self.Video_Widget)
self.play.clicked.connect(self.player.play)
self.playlist = QtMultimedia.QMediaPlaylist(self.player)
self.player.setPlaylist(self.playlist)
self.playlist.addMedia(
QtMultimedia.QMediaContent(QtCore.QUrl.fromLocalFile("F:/Fun/Songs/12.mp4"))
)
self.playlist.setCurrentIndex(0)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = VideoWindow()
w.show()
sys.exit(app.exec_())
I have two windows, first window is 'FirstWindow' and second is 'Calendar'. first window has one push button (pbSelectDate) and one label (lbDate). calendar window has calendar (CalendarBox) and one push button (pbSelect).
when i click select (pbSelectDate) from first window, calendar window pops up. I select the date and press select button (pbSelect) in the calendar window. i want to print the selected date to the label (lbDate) in the first window.
thanks in advance. :)
this is the first window script.
from PyQt5 import QtCore, QtGui, QtWidgets
from Calendar import Ui_CalendarWindow
class Ui_FirstWindow(object):
def Open_Calendar(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_CalendarWindow()
self.ui.setupUi(self.window)
self.window.show()
def setupUi(self, FirstWindow):
FirstWindow.setObjectName("FirstWindow")
FirstWindow.resize(654, 242)
self.centralwidget = QtWidgets.QWidget(FirstWindow)
self.centralwidget.setObjectName("centralwidget")
self.lbDate = QtWidgets.QLabel(self.centralwidget)
self.lbDate.setGeometry(QtCore.QRect(330, 70, 281, 131))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.lbDate.setFont(font)
self.lbDate.setLayoutDirection(QtCore.Qt.LeftToRight)
self.lbDate.setObjectName("lbDate")
self.pbSelectDate = QtWidgets.QPushButton(self.centralwidget)
self.pbSelectDate.setGeometry(QtCore.QRect(80, 100, 191, 61))
self.pbSelectDate.setObjectName("pbSelectDate")
self.pbSelectDate.clicked.connect(self.Open_Calendar)
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.lbDate.setText(_translate("FirstWindow", "Sep"))
self.pbSelectDate.setText(_translate("FirstWindow", "Select Date"))
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_())
this is the Calendar window script..
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_CalendarWindow(object):
def PickedDate(self,var):
self.selecteddate = self.CalendarBox.selectedDate()
#print(self.selecteddate.toString('MMM')+'-'+self.selecteddate.toString('yyyy'))
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.pbSelect = QtWidgets.QPushButton(self.centralwidget)
self.pbSelect.setGeometry(QtCore.QRect(160, 330, 181, 60))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pbSelect.setFont(font)
self.pbSelect.setObjectName("pbSelect")
self.pbSelect.clicked.connect(self.PickedDate)
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.pbSelect.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_())
Do not modify the code generated by Qt Designer but create another class that inherits from the appropriate widget and use the initial class to fill it.
QLabel.setText(const QString )
Setting the text clears any previous content.
from PyQt5 import QtCore, QtGui, QtWidgets
#from Calendar import Ui_CalendarWindow
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.pbSelect = QtWidgets.QPushButton(self.centralwidget)
self.pbSelect.setGeometry(QtCore.QRect(160, 330, 181, 60))
font = QtGui.QFont()
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pbSelect.setFont(font)
self.pbSelect.setObjectName("pbSelect")
# self.pbSelect.clicked.connect(self.PickedDate)
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.pbSelect.setText(_translate("CalendarWindow", "Select"))
class Ui_FirstWindow(object):
def setupUi(self, FirstWindow):
FirstWindow.setObjectName("FirstWindow")
FirstWindow.resize(654, 242)
self.centralwidget = QtWidgets.QWidget(FirstWindow)
self.centralwidget.setObjectName("centralwidget")
self.lbDate = QtWidgets.QLabel(self.centralwidget)
self.lbDate.setGeometry(QtCore.QRect(330, 70, 281, 131))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.lbDate.setFont(font)
self.lbDate.setLayoutDirection(QtCore.Qt.LeftToRight)
self.lbDate.setObjectName("lbDate")
self.pbSelectDate = QtWidgets.QPushButton(self.centralwidget)
self.pbSelectDate.setGeometry(QtCore.QRect(80, 100, 191, 61))
self.pbSelectDate.setObjectName("pbSelectDate")
# self.pbSelectDate.clicked.connect(self.Open_Calendar)
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.lbDate.setText(_translate("FirstWindow", "Sep"))
self.pbSelectDate.setText(_translate("FirstWindow", "Select Date"))
class CalendarWindow(QtWidgets.QMainWindow, Ui_CalendarWindow): # +
def __init__(self):
super(CalendarWindow, self).__init__()
self.setupUi(self)
class FirstWindow(QtWidgets.QMainWindow, Ui_FirstWindow): # +
def __init__(self):
super(FirstWindow, self).__init__()
self.setupUi(self)
self.pbSelectDate.clicked.connect(self.Open_Calendar)
def Open_Calendar(self):
self.window = CalendarWindow()
self.window.setupUi(self.window)
self.window.show()
self.window.pbSelect.clicked.connect(self.PickedDate)
def PickedDate(self): # , var
self.selecteddate = self.window.CalendarBox.selectedDate()
# print(self.selecteddate.toString('MMM')+'-'+self.selecteddate.toString('yyyy'))
self.lbDate.setText(self.selecteddate.toString('ddd-MMM-yyyy')) # <---
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = FirstWindow()
w.show()
sys.exit(app.exec_())
I am writing GUI app with the help of Qt Designer. The app consists of multiple windows, so when the user clicks the push button on one window, the next window should open and the current window should hide. I achieved this feature for going from window 1 to window 2. But I am not able to do this to move from window 2 to window 3. I think this might be because the window 2 gets user arguments.
Is there any way to hide the window which gets user arguments?
The code is as follows:
Class for main window:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import user_data
class MainWindow(object):
def user_data(self):
self.user_data_window = QtWidgets.QMainWindow()
self.next_window = user_data.Ui_UserData()
self.next_window.setupUi(self.user_data_window)
main_window.hide()
self.user_data_window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(140, 100, 361, 81))
font = QtGui.QFont()
font.setPointSize(24)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(260, 260, 75, 51))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.user_data)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 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", "Main Window"))
self.label.setText(_translate("MainWindow", " Manager"))
self.pushButton.setText(_translate("MainWindow", "Lets Start!"))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_window = QtWidgets.QMainWindow()
ui = MainWindow()
ui.setupUi(main_window)
main_window.show()
sys.exit(app.exec_())
user_data.py ---> window 2
from PyQt5 import QtCore, QtGui, QtWidgets
import start_recording
class Ui_UserData(object):
def start_record(self):
self.window = QtWidgets.QMainWindow()
self.ui = start_recording.Ui_StartRecording()
self.ui.setupUi(self.window)
"""
This is the part not working
"""
MainWindow.hide()
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.groupBox_speaker_data = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_speaker_data.setGeometry(QtCore.QRect(80, 30, 481, 381))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(84)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
self.groupBox_speaker_data.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.groupBox_speaker_data.setFont(font)
self.groupBox_speaker_data.setObjectName("groupBox_speaker_data")
self.label_name = QtWidgets.QLabel(self.groupBox_speaker_data)
self.label_name.setGeometry(QtCore.QRect(30, 60, 47, 13))
self.label_name.setObjectName("label_name")
self.label_age = QtWidgets.QLabel(self.groupBox_speaker_data)
self.label_age.setGeometry(QtCore.QRect(30, 120, 47, 13))
self.label_age.setObjectName("label_age")
self.label_id = QtWidgets.QLabel(self.groupBox_speaker_data)
self.label_id.setGeometry(QtCore.QRect(30, 90, 47, 13))
self.label_id.setObjectName("label_id")
self.label_gender = QtWidgets.QLabel(self.groupBox_speaker_data)
self.label_gender.setGeometry(QtCore.QRect(30, 150, 47, 13))
self.label_gender.setObjectName("label_gender")
self.lineEdit_name = QtWidgets.QLineEdit(self.groupBox_speaker_data)
self.lineEdit_name.setGeometry(QtCore.QRect(110, 60, 141, 20))
self.lineEdit_name.setObjectName("lineEdit_name")
self.lineEdit_id = QtWidgets.QLineEdit(self.groupBox_speaker_data)
self.lineEdit_id.setGeometry(QtCore.QRect(110, 90, 141, 20))
self.lineEdit_id.setObjectName("lineEdit_id")
self.spinBox_age = QtWidgets.QSpinBox(self.groupBox_speaker_data)
self.spinBox_age.setGeometry(QtCore.QRect(110, 120, 42, 22))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(18)
sizePolicy.setVerticalStretch(78)
self.spinBox_age.setSizePolicy(sizePolicy)
self.spinBox_age.setMinimum(18)
self.spinBox_age.setObjectName("spinBox_age")
self.comboBox_gender = QtWidgets.QComboBox(self.groupBox_speaker_data)
self.comboBox_gender.setGeometry(QtCore.QRect(110, 150, 69, 22))
self.comboBox_gender.setObjectName("comboBox_gender")
self.comboBox_gender.addItem("")
self.comboBox_gender.addItem("")
self.comboBox_gender.addItem("")
self.pushButton = QtWidgets.QPushButton(self.groupBox_speaker_data)
self.pushButton.setGeometry(QtCore.QRect(370, 330, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.start_record)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 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", "User Data"))
self.groupBox_speaker_data.setTitle(_translate("MainWindow", "Speaker Data"))
self.label_name.setText(_translate("MainWindow", "Name:"))
self.label_age.setText(_translate("MainWindow", "Age:"))
self.label_id.setText(_translate("MainWindow", "ID:"))
self.label_gender.setText(_translate("MainWindow", "Gender:"))
self.comboBox_gender.setItemText(0, _translate("MainWindow", "Male"))
self.comboBox_gender.setItemText(1, _translate("MainWindow", "Female"))
self.comboBox_gender.setItemText(2, _translate("MainWindow", "Unknown"))
self.pushButton.setText(_translate("MainWindow", "NEXT"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_UserData()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
start_recording.py ----> window 3
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_StartRecording(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(160, 120, 331, 91))
font = QtGui.QFont()
font.setFamily("Arial Black")
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(260, 200, 101, 51))
font = QtGui.QFont()
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(260, 260, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 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", "Lets begin"))
self.label_2.setText(_translate("MainWindow", "xxx!"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_StartRecording()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
class Ui_UserData(object):
def start_record(self):
self.window = QtWidgets.QMainWindow()
self.ui = start_recording.Ui_StartRecording()
self.ui.setupUi(self.window)
"""
This is the part not working
"""
MainWindow.hide()
^^^^^^^^ ---- there is no 'MainWindow' variable available to class, hence it doesn't know what to hide here.
Solution is:
class Ui_UserData(object):
def start_record(self):
self.window = QtWidgets.QMainWindow()
self.ui = start_recording.Ui_StartRecording()
self.ui.setupUi(self.window)
"""
This part is now working!
"""
self.mw.hide()
self.window.show()
def setupUi(self, MainWindow):
self.mw = MainWindow
# ^^^^^^^^ - You create class variable for mainwindow object