PyQt5 keep window on top by clicking button - python-3.x

I am trying to keep window by a Pin button. Here is the code:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QPushButton
from PyQt5 import QtCore
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'Keep Going'
self.width = 480
self.height = 360
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setFixedSize(self.width, self.height)
self.layout = QVBoxLayout()
self.button = QPushButton('PinTop')
self.button.setCheckable(True)
self.button.clicked.connect(self.winPinTop)
self.layout.addWidget(self.button)
# add tabs to widget
self.setLayout(self.layout)
self.show()
def winPinTop(self):
print('Pin')
button = self.sender()
if button.isChecked():
print('on top')
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
self.show()
else:
print('no top')
self.setWindowFlags(self.windowFlags())
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
The window can not keep on top after clicking the button. I don't know how to fix this.
Infomation may help:
Python 3.6.8
PyQt version: 5.13.0
Ubuntu 19.04
UPDATE
I replace QWidget with QMainWindow class to check this bug.
import sys
from PyQt5.QtWidgets import QMainWindow,QApplication, QWidget, QVBoxLayout, QPushButton, QPushButton
from PyQt5 import QtCore
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'Keep Going'
self.width = 480
self.height = 360
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
win = QWidget()
win.setFixedSize(self.width, self.height)
layout = QVBoxLayout()
button = QPushButton('PinTop')
button.setCheckable(True)
button.clicked.connect(self.winPinTop)
layout.addWidget(button)
# add tabs to widget
win.setLayout(layout)
self.setCentralWidget(win)
self.show()
def winPinTop(self):
print('Pin')
button = self.sender()
if button.isChecked():
print('on top')
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.Dialog )
print(self.windowFlags())
self.show()
else:
print('no top')
self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint )
print(self.windowFlags())
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
The problem is still here.
UPDATE 2019-9-11
After remove the show function. I can get the window on top.
def winPinTop(self):
print('Pin')
button = self.sender()
if button.isChecked():
print('on top')
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.Dialog )
print(self.windowFlags())
#self.show()
else:
print('no top')
self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint )
print(self.windowFlags())
#self.show()
But the window will close after I click the pin button. When I show this window, it stays on the top.
I don't know why this happened.
Reference: PyQt: Always on top

Related

PyQt5 - How can I delete the default empty field placed around a Tab widget?

I'm writing a GUI with PyQt5 and I realized that the Tab widgets have a default empty field placed around them (see the attached screenshot). How can I delete it?
Here is an example:
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QTabWidget, QVBoxLayout
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'PyQt5 tabs'
self.left = 0
self.top = 0
self.width = 300
self.height = 200
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.table_widget = MyTableWidget(self)
self.setCentralWidget(self.table_widget)
self.setStyleSheet("background-color:red")
self.show()
class MyTableWidget(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
self.layout = QVBoxLayout(self)
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tabs.resize(300,200)
self.tabs.addTab(self.tab1,"Tab 1")
self.tabs.addTab(self.tab2,"Tab 2")
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())

PyQt5: QLineEdit doesn't show inside a QGroupBox

I'm creating a PyQt5 application in which I want to put some of QLineEdit widgets inside a QGroupBox.
When I run my application, GroupBox is visible and LineEdit is not.
In CreateLinesEdit() I commented a line which sets the line edit visible, but it opens lineEdit in a new window.
from PyQt5.QtWidgets import QGroupBox, QApplication, QLineEdit, QVBoxLayout, QWidget, QHBoxLayout
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.InitWindow()
def InitWindow(self):
self.BoxLayout()
self.AddBox()
self.CreateLinesEdit()
self.show()
def BoxLayout(self):
self.groupBoxScreen = QGroupBox()
self.vbox = QVBoxLayout()
self.vbox_screenGame = QVBoxLayout()
self.hbox_lineEdit = QHBoxLayout()
def AddBox(self):
self.vbox_screenGame.addItem(self.hbox_lineEdit)
self.groupBoxScreen.setLayout(self.vbox_screenGame)
self.vbox.addWidget(self.groupBoxScreen)
self.setLayout(self.vbox)
def CreateLinesEdit(self):
self.lines_edit = []
for i in range(0, 4):
LineEdit = QLineEdit()
# LineEdit.setVisible(True)
self.lines_edit.append(LineEdit)
self.hbox_lineEdit.addWidget(self.lines_edit[i])
if __name__ == "__main__":
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())

How to make "finish" button in QStackedWidget

I trying to create "Finish" button in QStackedWidget.
In a "checkButtons" function i checking current page index and set click events and text. I tried to check it by class name, but it doesn't work too.
Here is a code:
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QDialog, QComboBox, QStackedWidget, QWidget,
QPushButton, QLabel, QVBoxLayout, QHBoxLayout, QStyle)
class Main(QDialog):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
# Main window setup
self.setWindowTitle("Stacked widget example")
self.setWindowIcon(self.style().standardIcon(QStyle.SP_FileDialogNewFolder))
self.setMinimumSize(400, 400)
self.setMaximumSize(640, 480)
self.rootVBox = QVBoxLayout()
self.rootHBox = QHBoxLayout()
self.rootHBox.addStretch()
self.rootVBox.addStretch()
self.pages = [FirstPage, SecondPage]
self.stacked = QStackedWidget(self)
for i in self.pages: self.stacked.addWidget(i(self))
self.pageState = True
self.buttonNext = QPushButton("Next")
self.buttonNext.clicked.connect(self.buttonNextConnect)
self.buttonBack = QPushButton("Back")
self.buttonBack.clicked.connect(self.buttonBackConnect)
self.rootHBox.addWidget(self.buttonBack)
self.rootHBox.addWidget(self.buttonNext)
self.rootVBox.addLayout(self.rootHBox)
self.setLayout(self.rootVBox)
def checkButtons(self):
print(self.stacked.currentIndex())
# I tried to check self.stacked.currentIndex() but it didn't work too
# if self.stacked.currentWidget().__class__ == self.pages[-1]:
if self.stacked.currentIndex() == len(self.pages) - 1:
self.buttonNext.setText("Finish")
self.buttonNext.clicked.connect(self.close)
elif self.stacked.currentIndex() < len(self.pages) - 1:
self.buttonNext.setText("Next")
self.buttonNext.clicked.connect(self.buttonNextConnect)
def buttonNextConnect(self):
self.stacked.setCurrentIndex(self.stacked.currentIndex() + 1)
self.checkButtons()
def buttonBackConnect(self):
self.stacked.setCurrentIndex(self.stacked.currentIndex() - 1)
self.checkButtons()
def finish(self):
self.close()
class FirstPage(QWidget):
def __init__(self, parent=None):
super(FirstPage, self).__init__(parent)
label = QLabel("First page")
rootVBox = QVBoxLayout()
rootHBox = QHBoxLayout()
rootHBox.addWidget(label)
rootVBox.addLayout(rootHBox)
self.setLayout(rootVBox)
class SecondPage(QWidget):
def __init__(self, parent=None):
super(SecondPage, self).__init__(parent)
label = QLabel("Second page")
rootVBox = QVBoxLayout()
rootHBox = QHBoxLayout()
rootHBox.addWidget(label)
rootVBox.addLayout(rootHBox)
self.setLayout(rootVBox)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())
If you try to press "next", "back" and then "next" again a program will be close. So, how can i fix it? Should i just make control buttons for each widget?
You must use the currentChanged signal of the QStackedWidget to know what page you are on and thus change the text, but in the buttonNextConnect slot you should check if you are already on the last page before switching to a new page, if you are then call to finish and if you do not change to another page
class Main(QDialog):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
# Main window setup
self.setWindowTitle("Stacked widget example")
self.setWindowIcon(self.style().standardIcon(QStyle.SP_FileDialogNewFolder))
self.setMinimumSize(400, 400)
self.setMaximumSize(640, 480)
rootVBox = QVBoxLayout(self)
rootHBox = QHBoxLayout()
rootHBox.addStretch()
rootVBox.addStretch()
self.pages = [FirstPage, SecondPage]
self.stacked = QStackedWidget(self)
for i in self.pages: self.stacked.addWidget(i(self))
self.buttonNext = QPushButton("Next")
self.buttonNext.clicked.connect(self.buttonNextConnect)
self.buttonBack = QPushButton("Back")
self.buttonBack.clicked.connect(self.buttonBackConnect)
rootHBox.addWidget(self.buttonBack)
rootHBox.addWidget(self.buttonNext)
rootVBox.addLayout(rootHBox)
self.stacked.currentChanged.connect(self.on_currentChanged)
def buttonNextConnect(self):
if self.stacked.currentIndex() == self.stacked.count() -1:
self.finish()
if self.stacked.currentIndex() < self.stacked.count() -1:
self.stacked.setCurrentIndex(self.stacked.currentIndex() + 1)
def buttonBackConnect(self):
if self.stacked.currentIndex() > 0:
self.stacked.setCurrentIndex(self.stacked.currentIndex() - 1)
def on_currentChanged(self, index):
if index == self.stacked.count() -1:
self.buttonNext.setText("Finish")
else:
self.buttonNext.setText("Next")
def finish(self):
self.close()
Another option is to use QWizard and QWizardPage:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Main(QtWidgets.QWizard):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
buttons = [
QtWidgets.QWizard.Stretch,
QtWidgets.QWizard.BackButton,
QtWidgets.QWizard.NextButton,
QtWidgets.QWizard.FinishButton
]
self.setButtonLayout(buttons)
self.addPage(FirstPage())
self.addPage(SecondPage())
class FirstPage(QtWidgets.QWizardPage):
def __init__(self, parent=None):
super(FirstPage, self).__init__(parent)
self.setTitle("First page")
class SecondPage(QtWidgets.QWizardPage):
def __init__(self, parent=None):
super(SecondPage, self).__init__(parent)
self.setTitle("Second page")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())

PyQt change picture with keyboard button

this is my first post here and I haven't seen it anywhere so hopefully it is ok. I am trying to change the displayed image with a keyboard click (think slideshow). This is my code so far:
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt
import os
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'Document Analysis'
self.left = 30
self.top = 30
self.width = 640
self.height = 480
self.imagenumber=0
self.initUI()
def keyPressEvent(self, event):
key=event.key()
if key==Qt.Key_Right:
self.imagenumber=self.imagenumber+1
self.showimage(self.imagenumber)
self.show()
else:
super(self).keyPressEvent(event)
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.showimage(0)
self.show()
def showimage(self,imagenumber):
label = QLabel(self)
directory = "C:\\Desktop\\Pictures"
imagelist = os.listdir(directory)
pixmap = QPixmap(directory + '\\' + imagelist[imagenumber])
label.setPixmap(pixmap)
self.resize(pixmap.width() + 500, pixmap.height())
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
It seems to be sort of getting somewhere, the first image displays just fine, and I know imagenumber does change, and when I stopped it from showing at first it resized the window but still didn't show the image. Any suggestions on what I am doing wrong?
This is part of a larger project which is the reason for the extra space on the side of the picture, but help would be appreciated.
You're close. Try the following...
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt
import os
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'Document Analysis'
self.left = 30
self.top = 30
self.width = 640
self.height = 480
self.imagenumber=0
self.initUI()
def keyPressEvent(self, event):
key=event.key()
if key==Qt.Key_Right:
self.imagenumber=self.imagenumber+1
self.showimage(self.imagenumber)
# self.show()
else:
super(self).keyPressEvent(event)
def initUI(self):
layout = QVBoxLayout()
self.setLayout(layout)
self.label = QLabel(self)
layout.addWidget(self.label)
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.showimage(0)
self.show()
def showimage(self,imagenumber):
# label = QLabel(self)
directory = "C:\\Desktop\\Pictures"
imagelist = os.listdir(directory)
pixmap = QPixmap(directory + '\\' + imagelist[imagenumber])
# label.setPixmap(pixmap)
self.label.setPixmap(pixmap)
self.resize(pixmap.width() + 500, pixmap.height())
# self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
Mainly, you need a persistent label. You also only need to call show() once.

pyqt4 Main window crashes after second window closes

I've got this simple program. Its has a button and when you press it a second window will come up.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
import urllib.request
class second_window(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self)
super(second_window, self).__init__(parent)
layout = QVBoxLayout()
button = QPushButton("close")
button.clicked.connect(self.button_clicked)
layout.addWidget(button)
self.setLayout(layout)
def button_clicked(self):
self.close()
class Main_window(QDialog):
def __init__(self):
QDialog.__init__(self)
layout = QVBoxLayout()
button = QPushButton("Second Window")
self.sec_window = second_window(self)
layout.addWidget(button)
button.clicked.connect(self.button_clicked)
self.setLayout(layout)
def button_clicked(self):
self.sec_window.exec_()
if __name__ == "__main__":
app = QApplication(sys.argv)
dl = window()
dl.show()
app.exec()
But sometimes if you close the Main_window just after you close the second_window it crashes and I get a message saying "Python.exe has stopped working".
can anyone help?
First Solution: Change self.sec_window = second_window(self) to self.sec_window = second_window() since it will not be necessary to clean because there is no relationship between second_window and Main_window. As the following code shows
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class second_window(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self)
super(second_window, self).__init__(parent)
layout = QVBoxLayout()
button = QPushButton("close")
button.clicked.connect(self.button_clicked)
layout.addWidget(button)
self.setLayout(layout)
def button_clicked(self):
self.close()
class Main_window(QDialog):
def __init__(self):
QDialog.__init__(self)
layout = QVBoxLayout()
button = QPushButton("Second Window")
self.sec_window = second_window()
layout.addWidget(button)
button.clicked.connect(self.button_clicked)
self.setLayout(layout)
def button_clicked(self):
self.sec_window.exec_()
if __name__ == "__main__":
app = QApplication(sys.argv)
dl = Main_window()
dl.show()
app.exec()
Second Solution: Add self.sec_window.deleteLater() on closeEvent(self, event) to be able to delete second_window
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class second_window(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self)
super(second_window, self).__init__(parent)
layout = QVBoxLayout()
button = QPushButton("close")
button.clicked.connect(self.button_clicked)
layout.addWidget(button)
self.setLayout(layout)
def button_clicked(self):
self.close()
class Main_window(QDialog):
def __init__(self):
QDialog.__init__(self)
layout = QVBoxLayout()
button = QPushButton("Second Window")
self.sec_window = second_window(self)
layout.addWidget(button)
button.clicked.connect(self.button_clicked)
self.setLayout(layout)
def button_clicked(self):
self.sec_window.exec_()
def closeEvent(self, event):
self.sec_window.deleteLater()
super().closeEvent(event)
if __name__ == "__main__":
app = QApplication(sys.argv)
dl = Main_window()
dl.show()
app.exec()

Resources