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)
Related
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 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()
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_()
I am new to pyqt5, I want pop-up a window and let the window can stay behinde the mainwindow and not clickable(smiliar like let the new window as an another process window)
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class Demo(QMainWindow):
def __init__(self):
super().__init__()
test_button = QPushButton('test')
test_button.clicked.connect(self.onClick)
self.setCentralWidget(test_button)
def onClick(self):
# dlg = QDialog(self)
dlg = QMainWindow(self)
dlg.setWindowFlag(Qt.WindowStaysOnTopHint, False)
dlg.show()
app = QApplication([])
demo = Demo()
demo.show()
app.exec()
the new window is always stay on the mainwindow, I need to the window stay behide it.
void QWidget::move(int x, int y)
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class Demo(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Main Window')
test_button = QPushButton('test')
test_button.clicked.connect(self.onClick)
self.setCentralWidget(test_button)
def onClick(self):
# dlg = QDialog(self)
dlg = QMainWindow(self)
dlg.setWindowTitle('Dialog Window')
dlg.move(self.geometry().x() + self.geometry().width() + 30, # <---
self.geometry().y() - 30)
dlg.setWindowFlag(Qt.WindowStaysOnTopHint, False)
dlg.show()
def sizeHint123(self):
return QSize(200, 200)
app = QApplication([])
demo = Demo()
demo.show()
app.exec()
Update
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class Demo(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Main Window')
self.setWindowFlags(Qt.WindowStaysOnTopHint) # +++
test_button = QPushButton('test')
test_button.clicked.connect(self.onClick)
self.setCentralWidget(test_button)
def onClick(self):
# dlg = QDialog(self)
self.dlg = QMainWindow() # --- self
self.dlg.setWindowTitle('Dialog Window')
self.dlg.move(self.geometry().x() + self.geometry().width() + 30,
self.geometry().y() - 30)
# dlg.setWindowFlag(Qt.WindowStaysOnTopHint, False)
self.dlg.show()
def sizeHint123(self):
return QSize(200, 200)
app = QApplication([])
demo = Demo()
demo.show()
app.exec()
After a QDialog is shown using either show() or exec_() I need to add some additional widgets dynamically. How can I do this?
Just call show() on your widgets:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class testDialogs(QWidget):
def __init__(self, parent=None):
super(testDialogs, self).__init__(parent)
self.verticalLayout = QVBoxLayout(self)
self.pushButton = QPushButton(self)
self.pushButton.setText("Open a Dialog")
self.pushButton1 = QPushButton(self)
self.pushButton1.setText("Add a Text Edit")
self.plainTextEdit = QPlainTextEdit(self)
self.plainTextEdit.appendPlainText("This is a Widget")
self.verticalLayout.addWidget(self.pushButton)
self.verticalLayout.addWidget(self.pushButton1)
self.verticalLayout.addWidget(self.plainTextEdit)
self.pushButton.clicked.connect(self.on_pushButton_clicked)
self.pushButton1.clicked.connect(self.on_pushButton1_clicked)
#pyqtSlot()
def on_pushButton_clicked(self):
dialog = QDialog(self)
verticalLayout = QVBoxLayout(dialog)
plainTextEdit = QPlainTextEdit(dialog)
plainTextEdit.appendPlainText("This is a Dialog")
buttonBox = QDialogButtonBox(dialog)
buttonBox.setOrientation(Qt.Horizontal)
buttonBox.setStandardButtons(QDialogButtonBox.Cancel|QDialogButtonBox.Ok)
buttonBox.setObjectName("buttonBox")
verticalLayout.addWidget(plainTextEdit)
verticalLayout.addWidget(buttonBox)
buttonBox.accepted.connect(dialog.close)
buttonBox.rejected.connect(dialog.close)
dialog.show()
#pyqtSlot()
def on_pushButton1_clicked(self):
plainTextEdit = QPlainTextEdit(self)
plainTextEdit.appendPlainText("This is another Text Edit")
self.verticalLayout.addWidget(plainTextEdit)
plainTextEdit.show()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
main = testDialogs()
main.show()
sys.exit(app.exec_())