Exit when Python gui button is clicked - python

I want to show the results by clicking on the button, but if I press this code, the program will end in two seconds.
And 'pursent.ui' is just a widget that hasn't been set up.
import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
form_class = uic.loadUiType("pursent.ui")[0]
class MyWindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.btneve)
def btneve(self):
self.statusbar.showMessage((int(self.lineEdit_2.text())-int(self.lineEdit.text()))/int(self.lineEdit.text())*100)
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()

void QStatusBar::showMessage(const QString &message, int timeout = 0)
Hides the normal status indications and displays the given message for the specified number of milli-seconds (timeout).
import sys
from PyQt5.QtWidgets import *
#from PyQt5 import uic
#form_class = uic.loadUiType("pursent.ui")[0]
class MyWindow(QMainWindow): #, form_class):
def __init__(self):
super().__init__()
# self.setupUi(self)
centralWidget = QWidget()
self.setCentralWidget(centralWidget)
self.lineEdit = QLineEdit()
self.lineEdit_2 = QLineEdit()
self.pushButton = QPushButton('Button')
self.pushButton.clicked.connect(self.btneve)
self.statusbar = self.statusBar() # = StatusBar(self)
grid = QGridLayout(centralWidget)
grid.addWidget(self.lineEdit)
grid.addWidget(self.lineEdit_2)
grid.addWidget(self.pushButton)
def btneve(self):
self.statusbar.showMessage(str( # + str
(int(self.lineEdit_2.text())-int(self.lineEdit.text()))/int(self.lineEdit.text())*100)
)
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = MyWindow()
myWindow.show()
app.exec_()

Related

PyQt5 custom widget split backghround?

I have had this happening multiple times. When I create a custom widget with an image in it all child widgets get seperated. How do I prevent that?
Full code that has the same outcome:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
class CustomWidget(QWidget):
def __init__(self):
super(CustomWidget, self).__init__()
self.layout = QHBoxLayout()
self.setLayout(self.layout)
self.setStyleSheet(r'QWidget {background-color:#353634;}')
self.name_label = QLabel('name')
self.qty_label = QLabel('999')
self.image_pix = QPixmap(IMAGEPATH)
self.image_pix = self.image_pix.scaled(48, 48)
self.icon = QLabel()
self.icon.setPixmap(self.image_pix)
self.layout.addWidget(self.icon)
self.layout.addWidget(self.name_label)
self.layout.addWidget(self.qty_label)
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.width, self.height = 425,350
self.resize(self.width, self.height)
custom_widget = CustomWidget()
self.setCentralWidget(custom_widget)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
win.show()
app.exec()
app.quit()

How to create a two or more Color Custom QPushButton in PyQt5?

How to Create a Custom Button with two or More color text and as well as in With double or single underline(in a particular letter)? I tried my level best. But the Blank button (no text) only appears.
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class MyButton(QPushButton):
def __init__ (self, mytext,parent=None):
super(MyButton,self).__init__()
self.mytext = mytext
def paintEvent(self, event):
document = QTextDocument()
document.setDocumentMargin(0)
document.setHtml(mytext)
mypixmap=QPixmap(document.size().tosize())
mypixmap.fill(Qt.transparent)
painter = QPainter(mypixmap)
document.drawContents(painter)
painter.end()
class CustomButton(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Grid layout Example")
self.setGeometry(100,100,400,400)
self.widget()
self.show()
def widget(self):
self.btn_sample = MyButton(QIcon("<h2><i>My sample</i> <font color=red>Button!</font></h2>"))
self.btn_sample.resize(20,20)
self.layout = QVBoxLayout()
self.layout.addWidget(self.btn_sample)
self.setLayout(self.layout)
def main():
app = QApplication(sys.argv)
mainwindow = CustomButton()
mainwindow.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class MyButton(QPushButton):
def __init__(self, Text, parent = None):
super(MyButton, self).__init__()
mydocument = QTextDocument()
mydocument.setDocumentMargin(0)
mydocument.setHtml(Text)
mypixmap = QPixmap(mydocument.size().toSize())
mypixmap.fill(Qt.transparent)
mypainter = QPainter(mypixmap)
mydocument.drawContents(mypainter)
mypainter.end()
myicon = QIcon(mypixmap)
self.setIcon(myicon)
self.setIconSize(mypixmap.size())
class mainwindow(QWidget):
def __init__(self , parent = None):
super(mainwindow, self).__init__()
self.setupgui()
def setupgui(self):
self.resize(800,600)
self.setWindowTitle('Custom Button With two Color Text')
newLayout = QHBoxLayout()
self.dashboard = MyButton("<h2><i>Dash Board</i> <font color=red>Qt!</font></h2>",self)
self.transcation = MyButton('<font color="red"><u>T</u></font><font color="black">ranscation</font>',self)
newLayout.addWidget(self.dashboard)
newLayout.addWidget(self.transcation)
self.setLayout(newLayout)
self.show()
def main():
app = QApplication(sys.argv)
ex = mainwindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

QCalendarWidget Renders Small

I am trying to use the QCalendarWidget but it doesn't render in the user interface as expected. The examples that I have seen show a calendar picker like object, but in my case I get a quite small rendering of a field. Here's what it looks like in the UI:
This is my first time using it so I am not sure if I am missing a step. Any thoughts on what I could be doing incorrectly? Here is the complete code being used:
from PyQt5.QtWidgets import QMainWindow, QCalendarWidget, QLabel
from PyQt5 import QtCore, QtWidgets, QtGui
import sys
class Example(QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
cal = QCalendarWidget(self)
cal.setGridVisible(True)
cal.move(20, 20)
cal.clicked[QtCore.QDate].connect(self.showDate)
self.lbl = QLabel(self)
date = cal.selectedDate()
self.lbl.setText(date.toString())
self.lbl.move(20, 200)
self.setGeometry(100,100,300,300)
self.setWindowTitle('Calendar')
self.show()
def showDate(self, date):
self.lbl.setText(date.toString())
def main():
app = QtWidgets.QApplication(sys.argv)
mainWin = Example()
mainWin.show()
sys.exit( app.exec_() )
if __name__ == '__main__':
main()
Use a layout, for example a QVBoxLayout, in the centralWidget of QMainWindow:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Example(QtWidgets.QMainWindow):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
cal = QtWidgets.QCalendarWidget(gridVisible=True)
cal.clicked.connect(self.showDate)
self.lbl = QtWidgets.QLabel()
date = cal.selectedDate()
self.lbl.setText(date.toString())
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
lay = QtWidgets.QVBoxLayout(central_widget)
lay.addWidget(cal)
lay.addWidget(self.lbl)
self.setGeometry(100, 100, 300, 300)
self.setWindowTitle("Calendar")
#QtCore.pyqtSlot(QtCore.QDate)
def showDate(self, date):
self.lbl.setText(date.toString())
def main():
app = QtWidgets.QApplication(sys.argv)
mainWin = Example()
mainWin.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()

Why does this code in PyQt allow me to leave all radio buttons unchecked?

I wrote a mini code using PyQt and I got surprised when I noticed that it allows me to leave all buttons on the left unchecked. I don't want this behaviour: one of them should always be selected. The right side of the window works, though. I don't really know why. Here's the code:
from PyQt5 import QtWidgets, QtGui, QtCore
class Win(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Win, self).__init__(parent)
top_layout = QtWidgets.QHBoxLayout()
top_layout_1 = QtWidgets.QHBoxLayout()
self.thresh_btns = [QtWidgets.QRadioButton('L1'),
QtWidgets.QRadioButton('L2'),
QtWidgets.QRadioButton('L3')]
self.thresh_btns[0].setChecked(True)
for btn in self.thresh_btns:
top_layout_1.addWidget(btn)
timestamp_groupbox = QtWidgets.QGroupBox('Timestamp')
top_layout_2 = QtWidgets.QVBoxLayout()
self.timestamp_current = QtWidgets.QRadioButton('Current')
self.timestamp_current.setChecked(True)
self.timestamp_target = QtWidgets.QRadioButton('Last')
top_layout_2.addWidget(self.timestamp_current)
top_layout_2.addWidget(self.timestamp_target)
timestamp_groupbox.setLayout(top_layout_2)
top_layout.addLayout(top_layout_1)
top_layout.addWidget(timestamp_groupbox)
self.setLayout(top_layout)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
ex = Win()
ex.show()
app.exec_()
If you want this behavior you have to use a QButtonGroup:
from PyQt5 import QtCore, QtGui, QtWidgets
class Win(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Win, self).__init__(parent)
top_layout = QtWidgets.QHBoxLayout()
top_layout_1 = QtWidgets.QHBoxLayout()
self.thresh_btns = [QtWidgets.QRadioButton('L1'),
QtWidgets.QRadioButton('L2'),
QtWidgets.QRadioButton('L3')]
group_button = QtWidgets.QButtonGroup(self) # <---
self.thresh_btns[0].setChecked(True)
for btn in self.thresh_btns:
top_layout_1.addWidget(btn)
group_button.addButton(btn) # <---
timestamp_groupbox = QtWidgets.QGroupBox('Timestamp')
top_layout_2 = QtWidgets.QVBoxLayout()
self.timestamp_current = QtWidgets.QRadioButton('Current')
self.timestamp_current.setChecked(True)
self.timestamp_target = QtWidgets.QRadioButton('Last')
top_layout_2.addWidget(self.timestamp_current)
top_layout_2.addWidget(self.timestamp_target)
timestamp_groupbox.setLayout(top_layout_2)
top_layout.addLayout(top_layout_1)
top_layout.addWidget(timestamp_groupbox)
self.setLayout(top_layout)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
ex = Win()
ex.show()
sys.exit(app.exec_())

PyQt5: Closing Window Command does not work

I have a problem and since I haven't found a similar question to my problem I am asking a question.
I am trying to close a window but the command for it doesn't work.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class Main(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
exitAct = QAction(QIcon('exit.ico'), 'Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.triggered.connect(qApp.quit) #This is the command I am refering to
self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(exitAct)
menu = QMenu()
menuItem1 = menu.addAction('File Explorer')
menuItem2 = menu.addAction('WritePad')
menuItem3 = menu.addAction('Settings')
startButton=QPushButton("Start", self)
startButton.setGeometry(0, 35, 100, 50)
startButton.setMenu(menu)
self.showFullScreen()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Main()
sys.exit(app.exec_())
Make it:
exitAct.triggered.connect(QtWidgets.qApp.quit)

Categories