Python YouTube downloader with pytube crashes when I search for video - python

I am making a YouTube video downloader with Python. I made the design with PyQt and edited the code in IDLE. Here is the code.
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'YTD.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
from pytube import YouTube
class Ui_MainWindow(object):
link = ''
video = None
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(354, 210)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
self.plainTextEdit.setGeometry(QtCore.QRect(10, 10, 261, 31))
self.plainTextEdit.setStyleSheet("font: 12pt \"MS Shell Dlg 2\";")
self.plainTextEdit.setPlainText("")
self.plainTextEdit.setObjectName("plainTextEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(270, 10, 71, 31))
self.pushButton.setObjectName("pushButton")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(20, 80, 51, 21))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(20, 99, 51, 21))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(20, 120, 51, 21))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(20, 140, 51, 21))
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(80, 80, 251, 21))
self.label_5.setText("")
self.label_5.setObjectName("label_5")
self.label_6 = QtWidgets.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(80, 100, 251, 21))
self.label_6.setText("")
self.label_6.setObjectName("label_6")
self.label_7 = QtWidgets.QLabel(self.centralwidget)
self.label_7.setGeometry(QtCore.QRect(80, 120, 251, 21))
self.label_7.setText("")
self.label_7.setObjectName("label_7")
self.label_8 = QtWidgets.QLabel(self.centralwidget)
self.label_8.setGeometry(QtCore.QRect(80, 140, 251, 21))
self.label_8.setText("")
self.label_8.setObjectName("label_8")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(270, 160, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.pushButton.clicked.connect(self.on_search_clicked)
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", "SEARCH"))
self.label.setText(_translate("MainWindow", "Title:"))
self.label_2.setText(_translate("MainWindow", "Author:"))
self.label_3.setText(_translate("MainWindow", "Views:"))
self.label_4.setText(_translate("MainWindow", "Likes:"))
self.pushButton_2.setText(_translate("MainWindow", "Download"))
#THIS IS THE METHOD THAT I WROTE MYSELF
def on_search_clicked(self):
global link
link = self.plainTextEdit.toPlainText()
global video
video = YouTube(link)
self.label_5.setText(video.title)
self.label_6.setText(video.author)
self.label_7.setText(str(video.views))
self.label_8.setText(str(video.likes))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
The problem is, when I provide a link in the plainTextEdit box and press the pushButton, the application crashes. There must be some problem in on_search_clicked() as it is the only method I wrote and the rest is auto generated.

Related

I want to add a Viewbox to my code to make an AutoPan for X axis (pyqtgraph)

This is a GUI where I plot data from serial communication.
I want to add an AutoPan for my GUI but I don't know how.
I read there is an autopan in Viewbox but don't know how to implement it.
The option of Autopan is available when I launch the gui buy right clicking on the gui and selectign it manually but I want it to be enabled by default .
from PyQt5 import QtCore, QtGui, QtWidgets
import pyqtgraph as pg
from random import randint
import serial
ser = serial.Serial('COM5',115200)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(769, 509)
MainWindow.setStyleSheet("\n""background-color: rgb(212, 212, 212);")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.graphicsView = PlotWidget(self.centralwidget)
self.graphicsView.setGeometry(QtCore.QRect(80, 110, 561, 271))
self.graphicsView.setObjectName("graphicsView")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(260, 30, 211, 31))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(20, 10, 151, 80))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(650, 110, 61, 21))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(650, 210, 121, 21))
self.label_4.setObjectName("label_4")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(650, 140, 69, 22))
self.comboBox.setStyleSheet("background-color: rgb(255, 255, 255);")
self.comboBox.setEditable(False)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_2.setGeometry(QtCore.QRect(650, 250, 69, 22))
self.comboBox_2.setStyleSheet("background-color: rgb(255, 255, 255);")
self.comboBox_2.setEditable(False)
self.comboBox_2.setObjectName("comboBox_2")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
self.splitter = QtWidgets.QSplitter(self.centralwidget)
self.splitter.setGeometry(QtCore.QRect(80, 410, 561, 51))
self.splitter.setOrientation(QtCore.Qt.Horizontal)
self.splitter.setObjectName("splitter")
self.pushButton = QtWidgets.QPushButton(self.splitter)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setStyleSheet("background-color: rgb(255, 255, 210);")
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.splitter)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.pushButton_2.setFont(font)
self.pushButton_2.setStyleSheet("background-color: rgb(255, 255, 210);")
self.pushButton_2.setObjectName("pushButton_2")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 769, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.graphicsView.setBackground('w')
self.retranslateUi(MainWindow)
self.comboBox.setCurrentIndex(1)
self.comboBox_2.setCurrentIndex(2)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Queen Monitor"))
self.label.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-size:18pt; font-weight:600;\">QUEEN MONITOR</span></p></body></html>"))
self.label_2.setText(_translate("MainWindow", "<html><head/><body><p><img src=\":/rf/queen.png\"/></p></body></html>"))
self.label_3.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-weight:600;\">Baud Rate</span></p></body></html>"))
self.label_4.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-weight:600;\">Sampling Frequency</span></p></body></html>"))
self.comboBox.setItemText(0, _translate("MainWindow", "4800"))
self.comboBox.setItemText(1, _translate("MainWindow", "115200"))
self.comboBox_2.setItemText(0, _translate("MainWindow", ".... Hz"))
self.comboBox_2.setItemText(1, _translate("MainWindow", ".... Hz"))
self.comboBox_2.setItemText(2, _translate("MainWindow", ".... Hz"))
self.pushButton.setText(_translate("MainWindow", "START"))
self.pushButton_2.setText(_translate("MainWindow", "CLEAR"))
self.pushButton.clicked.connect(lambda:self.start())
self.pushButton_2.clicked.connect(lambda:self.clear())
self.x = [0]*2 # 100 time points
self.y = [0]*2
pen = pg.mkPen(color=(0, 0, 255))
self.data_line = self.graphicsView.plot(self.x, self.y, pen=pen)
def start(self):
self.timer = QtCore.QTimer()
self.timer.setInterval(1)
self.timer.timeout.connect(self.draw)
self.timer.start()
def draw(self):
"""#self.x = self.x[1:] # Remove the first y element.
self.x.append(self.x[-1] + 1) # Add a new value 1 higher than the last.
#self.y = self.y[1:] # Remove the first
self.y.append( randint(0,200)) # Add a new random value.
self.data_line.setData(self.x, self.y) # Update the data."""
rawData = ser.readline().decode("ascii")
dataSplitted = rawData.split(",")
dataSplitted.pop()
dataSplitted.pop(0)
for self.data_raw in dataSplitted:
self.x.append(self.x[-1] + 1)
self.y.append(int(self.data_raw))
self.data_line.setData(self.x, self.y)
def clear(self):
self.graphicsView.clear()
from pyqtgraph import PlotWidget
import rf_rc
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_())

how to print continous data on pyqt5 text browser

I have created a manual web socket and I stucked.
Query) How to print continous/real time data receiving from server on text browser in pyqt5 GUI if connect (here name close) pushbutton is pressed (connection created) and if pushbutton is pressed again it should get toggled i.e. it should stop receiving data (connection close).
My code is
from PyQt5 import QtCore, QtGui, QtWidgets
from websocket import create_connection
class Ui_MainWindow(object):
def connect(self,MainWindow):
ws = create_connection("ws://192.168.0.205/ws")
self.textBrowser_2.append("Connection created")
while 1:
self.textBrowser_2.append(ws.recv())
print(ws.recv())
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(700, 70, 81, 61))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(50, 0, 191, 51))
font = QtGui.QFont()
font.setPointSize(12)
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(30, 60, 81, 51))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(160, 130, 141, 81))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(20, 200, 201, 61))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(690, 260, 81, 51))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(20, 330, 251, 21))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_5.setFont(font)
self.label_5.setObjectName("label_5")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(82, 70, 601, 61))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(20, 250, 651, 71))
self.lineEdit_2.setObjectName("lineEdit_2")
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(260, 150, 381, 51))
self.textBrowser.setObjectName("textBrowser")
self.textBrowser_2 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_2.setGeometry(QtCore.QRect(30, 360, 731, 192))
self.textBrowser_2.setObjectName("textBrowser_2")
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.pushButton.clicked.connect(self.connect)
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", "Close"))
self.label.setText(_translate("MainWindow", "Server loaction"))
self.label_2.setText(_translate("MainWindow", "URL"))
self.label_3.setText(_translate("MainWindow", "Status :"))
self.label_4.setText(_translate("MainWindow", "Request"))
self.pushButton_2.setText(_translate("MainWindow", "Send"))
self.label_5.setText(_translate("MainWindow", "Message Log"))
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_())
First of all, do not modify the code generated by QtDesigner so to apply my solution you must regenerate the .py file and call it gui.py: pyuic5 filename.ui -o gui.py -x.
On the other hand, do not use the websocket module since it is designed for synchronous logic, in this case it is better to use the QtWebSockets module that allows you to interact with the websockets using the Qt eventloop.
import sys
from PyQt5 import QtCore, QtWidgets, QtWebSockets
from gui import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.socket = QtWebSockets.QWebSocket()
self.socket.textMessageReceived.connect(self.handle_text_message_received)
self.ui.pushButton.clicked.connect(self.connect_to_websocket)
def connect_to_websocket(self):
if self.socket.state() == QtNetwork.QAbstractSocket.UnconnectedState:
url = "ws://192.168.0.205/ws"
self.socket.open(QtCore.QUrl(url))
elif self.socket.state() == QtNetwork.QAbstractSocket.ConnectedState:
self.socket.close()
def handle_text_message_received(self, message):
self.ui.textBrowser_2.append(message)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

implementing buttons' functions on main instead on script from pyqt5

I created a GUI using Qt designer, and converted it into .py file.
I used to write the functions directly in the GUI .py file, but I've been told to separate the code into two files: one with only the GUI commands as been converted from Qt designer, and Main - that would work all of the buttons and widgets of the GUI.
Now, I've managed to import the GUI from different file, and when I run the script the GUI does come up, but when I try to write functions I seem to have troubles.
This is my main:
from PyQt5 import QtWidgets
from mainwin import Ui_MainWindow
import sys
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super(ApplicationWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.exitbtn.clicked.connect(self.exitclicked) ***this is the connection and function that I'm trying to make 'em work***
def exitclicked(self,Dialog):
sys.exit()
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
application = ApplicationWindow()
application.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
And I don't know if it's relevant, but this is my GUI converted from Qt designer.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(895, 422)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.txtoutput = QtWidgets.QTextBrowser(self.centralwidget)
self.txtoutput.setGeometry(QtCore.QRect(150, 200, 471, 192))
self.txtoutput.setObjectName("txtoutput")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(50, 90, 69, 22))
self.comboBox.setObjectName("comboBox")
self.lbl = QtWidgets.QLabel(self.centralwidget)
self.lbl.setGeometry(QtCore.QRect(90, 30, 81, 16))
self.lbl.setObjectName("lbl")
self.lcdNumber = QtWidgets.QLCDNumber(self.centralwidget)
self.lcdNumber.setGeometry(QtCore.QRect(340, 170, 151, 23))
self.lcdNumber.setObjectName("lcdNumber")
self.getserialbtn = QtWidgets.QPushButton(self.centralwidget)
self.getserialbtn.setGeometry(QtCore.QRect(210, 50, 75, 23))
self.getserialbtn.setObjectName("getserialbtn")
self.chuckrealeasebtn = QtWidgets.QPushButton(self.centralwidget)
self.chuckrealeasebtn.setGeometry(QtCore.QRect(50, 120, 75, 23))
self.chuckrealeasebtn.setObjectName("chuckrealeasebtn")
self.runbtn = QtWidgets.QPushButton(self.centralwidget)
self.runbtn.setGeometry(QtCore.QRect(580, 70, 91, 61))
font = QtGui.QFont()
font.setPointSize(28)
font.setBold(True)
font.setWeight(75)
self.runbtn.setFont(font)
self.runbtn.setCheckable(False)
self.runbtn.setObjectName("runbtn")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(50, 50, 151, 31))
self.textEdit.setObjectName("textEdit")
self.stopbtn = QtWidgets.QPushButton(self.centralwidget)
self.stopbtn.setGeometry(QtCore.QRect(680, 70, 91, 61))
font = QtGui.QFont()
font.setPointSize(28)
font.setBold(True)
font.setWeight(75)
self.stopbtn.setFont(font)
self.stopbtn.setObjectName("stopbtn")
self.exitbtn = QtWidgets.QPushButton(self.centralwidget)
self.exitbtn.setGeometry(QtCore.QRect(740, 370, 75, 23))
self.exitbtn.setObjectName("exitbtn")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.lbl.setText(_translate("MainWindow", "Enter serial #"))
self.getserialbtn.setText(_translate("MainWindow", "Enter"))
self.chuckrealeasebtn.setText(_translate("MainWindow", "chuck"))
self.runbtn.setText(_translate("MainWindow", "Run"))
self.stopbtn.setText(_translate("MainWindow", "Stop"))
self.exitbtn.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_())
The error that I get when I try to run it is:
AttributeError: 'ApplicationWindow' object has no attribute 'exitbtn'
Would love if someone can write the right syntax to be able to implement functions through main.
Thank you so much :D
Try it:
main.py
from PyQt5 import QtWidgets
from mainwin import Ui_MainWindow
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super(ApplicationWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# self.exitbtn.clicked.connect(self.exitclicked)
self.ui.exitbtn.clicked.connect(self.exitclicked) # +
def exitclicked(self): # +
self.close() # +
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
application = ApplicationWindow()
application.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
mainwin.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(895, 422)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.txtoutput = QtWidgets.QTextBrowser(self.centralwidget)
self.txtoutput.setGeometry(QtCore.QRect(150, 200, 471, 192))
self.txtoutput.setObjectName("txtoutput")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(50, 90, 69, 22))
self.comboBox.setObjectName("comboBox")
self.lbl = QtWidgets.QLabel(self.centralwidget)
self.lbl.setGeometry(QtCore.QRect(90, 30, 81, 16))
self.lbl.setObjectName("lbl")
self.lcdNumber = QtWidgets.QLCDNumber(self.centralwidget)
self.lcdNumber.setGeometry(QtCore.QRect(340, 170, 151, 23))
self.lcdNumber.setObjectName("lcdNumber")
self.getserialbtn = QtWidgets.QPushButton(self.centralwidget)
self.getserialbtn.setGeometry(QtCore.QRect(210, 50, 75, 23))
self.getserialbtn.setObjectName("getserialbtn")
self.chuckrealeasebtn = QtWidgets.QPushButton(self.centralwidget)
self.chuckrealeasebtn.setGeometry(QtCore.QRect(50, 120, 75, 23))
self.chuckrealeasebtn.setObjectName("chuckrealeasebtn")
self.runbtn = QtWidgets.QPushButton(self.centralwidget)
self.runbtn.setGeometry(QtCore.QRect(580, 70, 91, 61))
font = QtGui.QFont()
font.setPointSize(28)
font.setBold(True)
font.setWeight(75)
self.runbtn.setFont(font)
self.runbtn.setCheckable(False)
self.runbtn.setObjectName("runbtn")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(50, 50, 151, 31))
self.textEdit.setObjectName("textEdit")
self.stopbtn = QtWidgets.QPushButton(self.centralwidget)
self.stopbtn.setGeometry(QtCore.QRect(680, 70, 91, 61))
font = QtGui.QFont()
font.setPointSize(28)
font.setBold(True)
font.setWeight(75)
self.stopbtn.setFont(font)
self.stopbtn.setObjectName("stopbtn")
self.exitbtn = QtWidgets.QPushButton(self.centralwidget)
self.exitbtn.setGeometry(QtCore.QRect(740, 370, 75, 23))
self.exitbtn.setObjectName("exitbtn")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.lbl.setText(_translate("MainWindow", "Enter serial #"))
self.getserialbtn.setText(_translate("MainWindow", "Enter"))
self.chuckrealeasebtn.setText(_translate("MainWindow", "chuck"))
self.runbtn.setText(_translate("MainWindow", "Run"))
self.stopbtn.setText(_translate("MainWindow", "Stop"))
self.exitbtn.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_())

Adding a simple clock to a GUI

I have some code for a very generic/blank GUI with some labels and pushbuttons and an LCD widget. I also have some code for a separate window which just uses an LCD widget to display the time. My question is how do I insert to code for the clock into the generic GUI code to make that LCD widget display the time?
from PyQt5.QtCore import QTime, QTimer
from PyQt5.QtWidgets import QApplication, QLCDNumber
class DigitalClock(QLCDNumber):
def __init__(self, parent=None):
super(DigitalClock, self).__init__(parent)
self.setSegmentStyle(QLCDNumber.Filled)
timer = QTimer(self)
timer.timeout.connect(self.showTime)
timer.start(1000)
self.showTime()
self.setWindowTitle("Digital Clock")
self.resize(150, 60)
def showTime(self):
time = QTime.currentTime()
text = time.toString('hh:mm')
if (time.second() % 2) == 0:
text = text[:2] + ' ' + text[3:]
self.display(text)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
clock = DigitalClock()
clock.show()
sys.exit(app.exec_())
That's the code for the simple clock and I want to insert that into the following to make the LCD widget display the time like this one does:
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.Clock = QtWidgets.QWidget(self.centralwidget)
self.Clock.setGeometry(QtCore.QRect(60, 420, 120, 80))
self.Clock.setObjectName("Clock")
self.DigitalClock = QtWidgets.QLCDNumber(self.centralwidget)
self.DigitalClock.setGeometry(QtCore.QRect(110, 190, 64, 23))
self.DigitalClock.setDigitCount(8)
self.DigitalClock.setObjectName("DigitalClock")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(100, 60, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(100, 110, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(100, 150, 75, 23))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(230, 60, 75, 23))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setGeometry(QtCore.QRect(220, 100, 75, 23))
self.pushButton_5.setObjectName("pushButton_5")
self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_6.setGeometry(QtCore.QRect(230, 150, 75, 23))
self.pushButton_6.setObjectName("pushButton_6")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(20, 60, 47, 13))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(20, 120, 47, 13))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(20, 160, 47, 13))
self.label_3.setObjectName("label_3")
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", "Sim Monitor"))
self.pushButton.setText(_translate("mainWindow", "PushButton"))
self.pushButton_2.setText(_translate("mainWindow", "PushButton"))
self.pushButton_3.setText(_translate("mainWindow", "PushButton"))
self.pushButton_4.setText(_translate("mainWindow", "PushButton"))
self.pushButton_5.setText(_translate("mainWindow", "PushButton"))
self.pushButton_6.setText(_translate("mainWindow", "PushButton"))
self.label.setText(_translate("mainWindow", "TextLabel"))
self.label_2.setText(_translate("mainWindow", "TextLabel"))
self.label_3.setText(_translate("mainWindow", "TextLabel"))
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_())
Simple solution:
Assuming that the first file is called DigitalClock.py and the second main.py then it is only necessary to add the import to main.py and change the line that the QLCDNumber creates:
main.py
from DigitalClock import DigitalClock
# ...
self.DigitalClock = DigitalClock(self.centralwidget) # QtWidgets.QLCDNumber(self.centralwidget)
Long solution:
Promote the QLCDNumber to use the DigitalClock, you could take the guide of this other answer where you would exchange QVideoWidget with DigitalClock and PyQt5.QtMultimediaWidgets with DigitalClock.
The other way is to create a widget plugin so that it can be used directly in Qt Designer.
DigitalClockFile.py
from PyQt5.QtCore import QTime, QTimer
from PyQt5.QtWidgets import QApplication, QLCDNumber
class DigitalClock(QLCDNumber):
def __init__(self, parent=None):
super(DigitalClock, self).__init__(parent)
self.setSegmentStyle(QLCDNumber.Filled)
self.setDigitCount(10)
timer = QTimer(self)
timer.timeout.connect(self.showTime)
timer.start(1000)
self.showTime()
self.setWindowTitle("Digital Clock")
self.resize(150, 60)
def showTime(self):
time = QTime.currentTime()
text = time.toString('hh:mm:ss')
if (time.second() % 2) == 0:
text = text[:2] + ' ' + text[3:5]+' '+text[6:]
self.display(text)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
clock = DigitalClock()
clock.show()
sys.exit(app.exec_())
generatedUI.py (from QtDesigner)
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
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.Clock = QtWidgets.QWidget(self.centralwidget)
self.Clock.setGeometry(QtCore.QRect(60, 420, 120, 80))
self.Clock.setObjectName("Clock")
# Remove DigitalClock FROM YOUR UI DESIGN,Since you already implemented your Digital Clock
# I would suggest regenerating .py from .ui after deleting digitalClock
# instead of commenting uot like me
# self.DigitalClock = QtWidgets.QLCDNumber(self.centralwidget)
# self.DigitalClock.setGeometry(QtCore.QRect(110, 190, 64, 23))
# self.DigitalClock.setDigitCount(8)
# self.DigitalClock.setObjectName("DigitalClock")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(100, 60, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(100, 110, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(100, 150, 75, 23))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(230, 60, 75, 23))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setGeometry(QtCore.QRect(220, 100, 75, 23))
self.pushButton_5.setObjectName("pushButton_5")
self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_6.setGeometry(QtCore.QRect(230, 150, 75, 23))
self.pushButton_6.setObjectName("pushButton_6")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(20, 60, 47, 13))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(20, 120, 47, 13))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(20, 160, 47, 13))
self.label_3.setObjectName("label_3")
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", "Sim Monitor"))
self.pushButton.setText(_translate("mainWindow", "PushButton"))
self.pushButton_2.setText(_translate("mainWindow", "PushButton"))
self.pushButton_3.setText(_translate("mainWindow", "PushButton"))
self.pushButton_4.setText(_translate("mainWindow", "PushButton"))
self.pushButton_5.setText(_translate("mainWindow", "PushButton"))
self.pushButton_6.setText(_translate("mainWindow", "PushButton"))
self.label.setText(_translate("mainWindow", "TextLabel"))
self.label_2.setText(_translate("mainWindow", "TextLabel"))
self.label_3.setText(_translate("mainWindow", "TextLabel"))
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_())
main.py
from DigitalClockFile import DigitalClock
from generatedUI import Ui_mainWindow
from PyQt5.QtCore import QTime, QTimer
from PyQt5.QtWidgets import QApplication, QLCDNumber
from PyQt5 import QtWidgets,QtCore
class transformedUI(Ui_mainWindow):
def __init__(self):
super(transformedUI,self).__init__()
def setupUi(self,mainWindow):
super(transformedUI,self).setupUi(mainWindow)
self.DigitalClock=DigitalClock(self.centralwidget)
self.DigitalClock.setGeometry(QtCore.QRect(110, 190, 200, 100))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mainWindow = QtWidgets.QMainWindow()
ui = transformedUI()
ui.setupUi(mainWindow)
mainWindow.show()
sys.exit(app.exec_())
Add all the three files in the single directory.Since you already implement DigitalClock dont add the clock in thi UI just inherit class generated by QtDEsigner and add your clock in the inherited transformedUI.py and simply running the main.py will achieve desired task.Hope it helps

PyQt5 window keeps exiting when opening a new window

# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created by: PyQt5 UI code generator 5.8.2
#
# 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(733, 497)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.My_2 = QtWidgets.QPushButton(self.centralWidget)
self.My_2.setGeometry(QtCore.QRect(0, 30, 101, 41))
self.My_2.setStyleSheet("")
self.My_2.setText("")
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("../../Major Project/Add a game button 1.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.My_2.setIcon(icon)
self.My_2.setIconSize(QtCore.QSize(100, 100))
self.My_2.setObjectName("My_2")
self.My_3 = QtWidgets.QPushButton(self.centralWidget)
self.My_3.setGeometry(QtCore.QRect(0, 80, 101, 41))
self.My_3.setObjectName("My_3")
self.My_3.clicked.connect(self.editbutton) #EDIT BUTTON
self.My_4 = QtWidgets.QPushButton(self.centralWidget)
self.My_4.setGeometry(QtCore.QRect(60, 260, 31, 31))
self.My_4.setText("")
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("../../Major Project/settings button.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.My_4.setIcon(icon1)
self.My_4.setIconSize(QtCore.QSize(32, 32))
self.My_4.setObjectName("My_4")
self.My_5 = QtWidgets.QPushButton(self.centralWidget)
self.My_5.setGeometry(QtCore.QRect(0, 210, 101, 41))
self.My_5.setObjectName("My_5")
self.line_3 = QtWidgets.QFrame(self.centralWidget)
self.line_3.setGeometry(QtCore.QRect(100, 0, 20, 341))
self.line_3.setFrameShape(QtWidgets.QFrame.VLine)
self.line_3.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_3.setObjectName("line_3")
self.timeEdit = QtWidgets.QTimeEdit(self.centralWidget)
self.timeEdit.setGeometry(QtCore.QRect(0, 300, 101, 22))
self.timeEdit.setObjectName("timeEdit")
self.scrollArea = QtWidgets.QScrollArea(self.centralWidget)
self.scrollArea.setGeometry(QtCore.QRect(130, 30, 571, 271))
self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 552, 269))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.graphicsView_2 = QtWidgets.QGraphicsView(self.scrollAreaWidgetContents)
self.graphicsView_2.setGeometry(QtCore.QRect(160, 30, 101, 81))
self.graphicsView_2.setObjectName("graphicsView_2")
self.graphicsView = QtWidgets.QGraphicsView(self.scrollAreaWidgetContents)
self.graphicsView.setGeometry(QtCore.QRect(30, 30, 101, 81))
self.graphicsView.setObjectName("graphicsView")
self.line_2 = QtWidgets.QFrame(self.scrollAreaWidgetContents)
self.line_2.setGeometry(QtCore.QRect(10, 10, 531, 20))
self.line_2.setFrameShape(QtWidgets.QFrame.HLine)
self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_2.setObjectName("line_2")
self.pushButton = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.pushButton.setGeometry(QtCore.QRect(40, 110, 81, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.pushButton_2.setGeometry(QtCore.QRect(170, 110, 81, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 733, 21))
self.menuBar.setObjectName("menuBar")
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtWidgets.QToolBar(MainWindow)
self.mainToolBar.setObjectName("mainToolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
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.My_3.setText(_translate("MainWindow", "Edit"))
self.My_5.setText(_translate("MainWindow", "Help"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
def editbutton(self):
if __name__ == "__main__":
import sys, editui
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = editui.Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
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 need help with this: the window keeps exiting when My_3 button is pressed, after both windows opening for a split second. editui.py is a generic auto-generated file with PyQt, so there's that.
If anyone can help me out, thanks!

Categories