I have test.qml file and in PyQt5 am using
self.view = QQuickView()
self.view.setResizeMode(QQuickView.SizeRootObjectToView)
self.view.setSource(QUrl.fromLocalFile(os.path.join(os.path.dirname(__file__),'test.qml')))
I have a button when it is clicked it overwrite the test.qml and call view again to read the test.qml file but it still display the old test.qml not the new one. Why?
Is it something with QQmlEngine clearComponentCache()
sample of the code:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QUrl
from PyQt5.QtQuick import QQuickView
import os
class Ui_MainWindow(object):
def add(self):
# call a function in a nother python file that will open test.qml and overwite it
# when i call the test.qml again it will show the old version not the new one
self.view.setSource(QUrl.fromLocalFile(os.path.join(os.path.dirname(__file__),'root.qml')))
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(420, 380, 113, 32))
self.pushButton.setObjectName("pushButton")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(170, 50, 551, 311))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
vbox = QtWidgets.QVBoxLayout(self.frame)
# loading the qml for the first time
self.view = QQuickView()
self.view.setResizeMode(QQuickView.SizeRootObjectToView)
self.view.setSource(QUrl.fromLocalFile(os.path.join(os.path.dirname(__file__),'root.qml')))
self.widget = QtWidgets.QWidget.createWindowContainer(self.view)
vbox.addWidget(self.widget)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.pushButton.clicked.connect(self.add)
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_())
I have not worked with QQuickView yet but rather prefer QQmlApplicationEngine. With it, I used an approach similar to the one outlined here.
I guess you could add a method like this to your main window class:
def reload(self):
self.view.engine().clearComponentCache()
self.view.setSource(
QUrl.fromLocalFile(
os.path.join(os.path.dirname(__file__),'root.qml')))
The clearComponentCache() method causes the engine to discard any previously loaded QML documents. On the next load operation (e.g. by using setSource() on your view), the engine will hence be force to reload the file from disk.
Related
I am using Python and I have a main window with a button that opens another window which contain stackedWidget, when the user click "Finish" on the second window I want to close it. but when i try to use self.close() method it closing the main windows as well.
here is the part in my code that handle the Finish click event on the second window:
def click_next(self):
if self.next.text()!="Finish>":
if self.stackedWidget.currentIndex()!=2:
self.stackedWidget.setCurrentIndex(self.stackedWidget.currentIndex()+1)
else:
self.next.setText("Finish>")
self.stackedWidget.setCurrentIndex(self.stackedWidget.currentIndex() + 1)
else:
self.close()
Is there another way to close one window and keep the main open.
Thanks.
So for the example I created 2 windows ( main and screen1), the button in main will open screen1 and the button in screen1 expected to close only screen1 but it close both windows.
code for main window:
from PyQt5 import QtCore, QtGui, QtWidgets
from screen1 import Ui_screen1
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(584, 567)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.button = QtWidgets.QPushButton(self.centralwidget, clicked=lambda:
self.gotoscreen1())
self.button.setGeometry(QtCore.QRect(220, 260, 131, 51))
self.button.setObjectName("buton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 584, 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 gotoscreen1(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_screen1()
self.ui.setupUi(self.window)
self.window.show()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.button.setText(_translate("MainWindow", "go to screen1"))
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_())
code for screen1:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_screen1(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(384, 567)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.button = QtWidgets.QPushButton(self.centralwidget, clicked=lambda: self.close())
self.button.setGeometry(QtCore.QRect(120, 260, 131, 51))
self.button.setObjectName("buton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 584, 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 close(self):
self.close()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.button.setText(_translate("MainWindow", "Close"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_screen1()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
The problem isn't that it is closing both windows, it's that your whole app is crashing because it has reached the maximum recursion depth.
The issue is with your close method.
def close(self):
self.close()
All this is doing is calling itself over and over and over again until it crashes the program.
To fix this simply remove the close method completely, then in your setupUi method change the line that creates the self.button to:
self.button = QtWidgets.QPushButton(self.centralwidget, clicked=MainWindow.close)
I would also recommend subclassing QMainWindow instead of using the uic-like design.
I am trying to get the date from a calendar widget to get printed into a textbox. The calendar widget and the textbox are in the same class but the print function is in a different function. all in the same class.
here is the code:
from PyQt5 import QtCore, QtGui, QtWidgets
import calendar
I have the printDate function to print the date
class Ui_Calendar(object):
def printDate(self, qDate):
date =('{0}-{1}-{2}'.format(qDate.month(), qDate.day(), qDate.year()))
self.setupUi.textedit.setText(date)
Here in this function i have the calendar widget and the textbox:
def setupUi(self, MainWindow):
calendar widget:
self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
self.calendarWidget.setGeometry(QtCore.QRect(0, 0, 392, 236))
self.calendarWidget.setObjectName("calendarWidget")
self.calendarWidget.clicked.connect(self.printDate)
textbox:
self.textedit = QtWidgets.QLineEdit(self.centralwidget)
self.textedit.setGeometry(QtCore.QRect(20,245,80,30))
self.textedit.setObjectName("textedit")
I get the calendar widget to open when button clicked. I also can get the date printed on python shell but cannot the date on the textbox. As soon as i click the date on the calendar widget the program closes itself. i dont get any error msg.
I am new to programming and just started learning. Please help me
here is my whole code:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Calendar(object):
def printDate(self, qDate):
date =('{0}-{1}-{2}'.format(qDate.month(), qDate.day(), qDate.year()))
#print(date)
self.setupUi.textedit.setText(self.date.toStrings())
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(395, 310)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
self.calendarWidget.setGeometry(QtCore.QRect(0, 0, 392, 236))
self.calendarWidget.setObjectName("calendarWidget")
#self.calendarWidget.clicked.connect(lambda dateval:print(dateval))
self.calendarWidget.clicked.connect(self.printDate)
MainWindow.setCentralWidget(self.centralwidget)
self.textedit = QtWidgets.QLineEdit(self.centralwidget)
self.textedit.setGeometry(QtCore.QRect(20,245,80,30))
self.textedit.setObjectName("textedit")
#self.printDate(self.label.setText(date))
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","date"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_Calendar()
ui.setupUi(MainWindow)
MainWindow.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.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Calendar(object):
# def printDate(self, qDate):
# date =('{0}-{1}-{2}'.format(qDate.month(), qDate.day(), qDate.year()))
# #print(date)
# self.setupUi.textedit.setText(self.date.toStrings())
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(395, 310)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
self.calendarWidget.setGeometry(QtCore.QRect(0, 0, 392, 236))
self.calendarWidget.setObjectName("calendarWidget")
#self.calendarWidget.clicked.connect(lambda dateval:print(dateval))
# self.calendarWidget.clicked.connect(self.printDate)
MainWindow.setCentralWidget(self.centralwidget)
self.textedit = QtWidgets.QLineEdit(self.centralwidget)
self.textedit.setGeometry(QtCore.QRect(20,245,80,30))
self.textedit.setObjectName("textedit")
#self.printDate(self.label.setText(date))
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","date"))
class Calendar(QtWidgets.QMainWindow, Ui_Calendar):
def __init__(self):
super().__init__()
self.setupUi(self)
self.calendarWidget.clicked.connect(self.printDate)
def printDate(self, qDate):
date =('{0}-{1}-{2}'.format(qDate.month(), qDate.day(), qDate.year()))
# self.setupUi.textedit.setText(self.date.toStrings())
self.textedit.setText(date)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
# MainWindow = QtWidgets.QMainWindow()
# ui = Ui_Calendar()
# ui.setupUi(MainWindow)
# MainWindow.show()
w = Calendar()
w.show()
sys.exit(app.exec_())
I Want this app to refresh the 'label' data (Which is a datetime) when i click the Refresh Button.
I tried pushButton.clicked.connect(ui.setupUi(MainWindow)), but it is not working, im out of clue how to do that.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
import datetime
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(200, 200)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(30, 30, 121, 61))
self.label.setObjectName("label")
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):
import datetime
dt = str(datetime.datetime.now())
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", dt))
pushButton = QtWidgets.QPushButton(self.centralwidget)
pushButton.setGeometry(QtCore.QRect(120, 150, 75, 23))
pushButton.setObjectName("pushButton")
# pushButton.clicked.connect(ui.setupUi(MainWindow))
pushButton.setText(_translate("MainWindow", "Refresh"))
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_())
After clicking the Refresh button the time should update.
You really shouldn't re-initialize the ui for that, but just set the text for the label.
Also, since datetime.datetime.now() is "dynamic", you should use a lambda or, better, a dedicated slot.
The "simpler" solution, based on your code, is use this where you have the commented code:
pushButton.clicked.connect(lambda: self.label.setText(str(datetime.datetime.now())))
Or, sometimes better, the same thing in a specific method:
def retranslateUi(self, MainWindow):
# ...
pushButton.clicked.connect(self.setCurrentDate)
# ...
def setCurrentDate(self):
self.label.setText(str(datetime.datetime.now()))
You were there almost. Using pushButton.clicked.connect() is correct, but you don't need to recall the initUI function.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
import datetime
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(200, 200)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(30, 30, 121, 61))
self.label.setObjectName("label")
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):
dt = str(datetime.datetime.now())
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", dt))
pushButton = QtWidgets.QPushButton(self.centralwidget)
pushButton.setGeometry(QtCore.QRect(120, 150, 75, 23))
pushButton.setObjectName("pushButton")
pushButton.clicked.connect(lambda: self.update_time())
pushButton.setText(_translate("MainWindow", "Refresh"))
def update_time(self):
self.label.setText(str(datetime.datetime.now()))
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_())
I made it in a seperate function so you can add more things to it if you want. You could also make pushButton a class member so you can check the state in update_time(). Or you could pass it as an function argument.
I am designing GUI using qt designer. I am converting the UI into code and running it in pycharm. When I create a Main Window in qt designer with 2 Menu:
Flie-> [Import,Exit]
File is the main Menu, Import and Exit are submenu. Only the first submenu is shown. When I am designing in qt-designer, it is shown, but when I run it in qt-designer, only the first one is shown. The generated code is as follows:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.12.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(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
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.actionImport = QtWidgets.QAction(MainWindow)
self.actionImport.setObjectName("actionImport")
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
self.menuFile.addAction(self.actionImport)
self.menuFile.addAction(self.actionExit)
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.menuFile.setTitle(_translate("MainWindow", "File"))
self.actionImport.setText(_translate("MainWindow", "Import"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
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_())
If I try to run this code in Pycharm, it also shows only the first menu.
What should I do when I want the variable inside a spinbox widget to display in the LCD widget at a click of the pushbutton widget?
Code form converting .ui to .py. Now I don't know what else to do. please edit as you see fit. :-)
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'C:\Users\CpE-18\Desktop\mk2.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# 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(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.spinBox = QtWidgets.QSpinBox(self.centralwidget)
self.spinBox.setGeometry(QtCore.QRect(320, 250, 51, 31))
self.spinBox.setObjectName("spinBox")
self.lcdNumber = QtWidgets.QLCDNumber(self.centralwidget)
self.lcdNumber.setGeometry(QtCore.QRect(270, 110, 141, 101))
self.lcdNumber.setObjectName("lcdNumber")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(310, 320, 75, 23))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 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", "OK"))
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_())
You have to do the following:
Create a function that is called when the button is pressed, for this the clicked signal is used.
Obtain the value of the QSpinBox using the value() method.
Set the value in the QLCDNumber through the display() method.
# ...
class Ui_MainWindow(object):
# ...
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.on_clicked)
#QtCore.pyqtSlot()
def on_clicked(self):
self.lcdNumber.display(self.spinBox.value())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())