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/'])
Related
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_())
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.