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.
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.
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_())
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.
I don´t know how to search for, so I will ask here.
What I wanna do is, to create a Qplaintextedit within a Window.
Now I execute my programm and resize it as I want, but my qplaintextedit
isn´t changing his size.
There is any function or something I can do, to set them in relation?
Greetings
Edit:
My Code if it helps:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(799, 592)
MainWindow.setContextMenuPolicy(QtCore.Qt.PreventContextMenu)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.desk = QtWidgets.QPlainTextEdit(self.centralwidget)
self.desk.setGeometry(QtCore.QRect(10, 10, 781, 551))
self.desk.setObjectName("desk")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 799, 21))
self.menubar.setObjectName("menubar")
self.menuDatei = QtWidgets.QMenu(self.menubar)
self.menuDatei.setObjectName("menuDatei")
self.menuHilfe = QtWidgets.QMenu(self.menubar)
self.menuHilfe.setObjectName("menuHilfe")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.openFile = QtWidgets.QAction(MainWindow)
self.openFile.setObjectName("openFile")
self.actionSpeichern = QtWidgets.QAction(MainWindow)
self.actionSpeichern.setObjectName("actionSpeichern")
self.actionSpeichern_als = QtWidgets.QAction(MainWindow)
self.actionSpeichern_als.setObjectName("actionSpeichern_als")
self.saveFile = QtWidgets.QAction(MainWindow)
self.saveFile.setObjectName("saveFile")
self.saveFileAs = QtWidgets.QAction(MainWindow)
self.saveFileAs.setObjectName("saveFileAs")
self.Credits = QtWidgets.QAction(MainWindow)
self.Credits.setObjectName("Credits")
self.menuDatei.addAction(self.openFile)
self.menuDatei.addSeparator()
self.menuDatei.addAction(self.saveFile)
self.menuDatei.addAction(self.saveFileAs)
self.menuHilfe.addAction(self.Credits)
self.menubar.addAction(self.menuDatei.menuAction())
self.menubar.addAction(self.menuHilfe.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.menuDatei.setTitle(_translate("MainWindow", "Datei"))
self.menuHilfe.setTitle(_translate("MainWindow", "Hilfe"))
self.openFile.setText(_translate("MainWindow", "Öffnen"))
self.actionSpeichern.setText(_translate("MainWindow", "Speichern"))
self.actionSpeichern_als.setText(_translate("MainWindow", "Speichern als..."))
self.saveFile.setText(_translate("MainWindow", "Speichern"))
self.saveFileAs.setText(_translate("MainWindow", "Speichern als..."))
self.Credits.setText(_translate("MainWindow", "Credits ©"))
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 need to add layout to MainWindow. It seems to me that you're using QtDesigner. If so, then right click MainWindow in object browser, choose Lay out and select an appropriate layout. The horizontal or vertical will be ok to start with.
Or you may add layout in code. I hope this will work in Qt5:
self.layout = QtWidgets.QVBoxLayout(self)
self.layout.addWidget(self.desk)
self.centralwidget.setLayout(self.layout)
I am trying to get my first Python learning app completed, I used PyQT5 designer to make a basic UI and used the following to test things out and I get the NameError: global name 'self' is not defined error. My question is what is the correct procedure to update txtProg (Text Editor) in GUI.
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'main.ui'
#
# Created: Sun Dec 1 20:19:03 2013
# by: PyQt5 UI code generator 5.1.1
#
# WARNING! All changes made in this file will be lost!
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from mydl import TheDLClass
def progress_hook(txt):
self.txtProg.setText(txt)
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.txtProg = QtWidgets.QPlainTextEdit(self.centralwidget)
self.txtProg.setGeometry(QtCore.QRect(50, 80, 661, 431))
self.txtProg.setObjectName("txtProg")
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)
self.launchit(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "My Downloader"))
def launchit(self, MainWindow):
MainWindow.setWindowTitle('Launching..')
with TheDLClass() as dl:
dl.fd.add_progress_hook(progress_hook)
dl.download(['http://myurl.com/'])
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 can't access self without explicitly pass it. (using method or lambda)
More preferably, define progress_hook as instance method:
def progress_hook(self, txt):
self.txtProg.setText(txt)
and bind it as follow:
def launchit(self, MainWindow):
MainWindow.setWindowTitle('Launching..')
with TheDLClass() as dl:
dl.fd.add_progress_hook(self.progress_hook) # <----
dl.download(['http://myurl.com/'])