I have this code:
def initUI(self):
mainLayout = QVBoxLayout()
# Temporary: Dialog Testing Button #
self.buttonTest = button('Dialog Testing')
self.buttonTest.clicked.connect(self.TestFile)
mainLayout.addWidget(self.buttonTest)
self.setLayout(mainLayout)
# Connects to dialogTesting Button
def dialogMessage(self, message):
dialog = QMessageBox(self)
dialog.setWindowTitle('Sample Text')
dialog.setText(message)
dialog.show()
# Connects with dialogMessage class.
def TestFile(self):
self.dialogMessage("When will non-OK buttons appear?")
return
I get results like this:
How can I change what buttons appear in the popup?
You can change the buttons by using an argument for the buttons parameter when you instantiate a QMessageBox. You use the Standard Button constants. Here is a silly example:
dialog = QMessageBox(self, buttons=QMessageBox.Ok+QMessageBox.Save+QMessageBox.Yes )
If you don't like the choices for standard buttons, you can make your own using addButton. This answer shows how to use addButton.
Related
I have a list of Qt buttons like this: self.buttons = [button1, button2, button3]
When one is clicked, I want all the buttons that come before the one that was clicked in the list to change their colors.
I made a for loop to loop through the buttons and connect each one to a function that I defined, but when I click a button and the connected function runs, it does not know the order of the button in the buttons list, therefore I cannot make the other buttons change colors. I was thinking that I need to somehow pass the button's id or something to the function but couldn't figure out how to do it as I cannot pass arguments to the connected function: self.button1.clicked.connect(self.change_color)
One argument is automatically passed to the connected function by Qt itself but it is the main window and it does not help my situation:
def change_color(i):
print(i)
Output when clicked:
<__main__.Main_Window(0x6000019e0000, name="MainWindow") at 0x11df5ccc0>
Add all the buttons to a QButtonGroup (using their index as id) and then connect to the group's idClicked signal. This will automatically send the index of the clicked button. Below is a simple demo that shows how to do it:
import sys, random
from PySide2 import QtCore, QtWidgets
class Window(QtWidgets.QWidget):
def __init__(self):
super().__init__()
layout = QtWidgets.QVBoxLayout(self)
self.buttons = QtWidgets.QButtonGroup(self)
for index in range(1, 11):
button = QtWidgets.QPushButton(f'Button {index}')
self.buttons.addButton(button, index)
layout.addWidget(button)
self.buttons.idClicked.connect(self.handleButtons)
def handleButtons(self, index):
color = f'#{random.randint(0, 0xFFFFFF):06x}'
for index in range(1, index):
self.buttons.button(index).setStyleSheet(f'background: {color}')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setWindowTitle('Test')
window.show()
sys.exit(app.exec_())
I want to use a dialog together with the SysTrayIcon.
The reason for the app is running in a systray, and show the dialog when you press the icon button. The app will close entirely just from the menu option.
Problem to solve: when lose focus (eg:clicking outisde) he dialog should hide even if the dialog had a button to display a modal window
Please test the sample code, as it illustrates everything.
from PySide2.QtWidgets import *
import sys
message = '''
This is a dialog we wanna use together with the SysTrayIcon
The reason of the app is running in a systray, and show the
dialog when you press the icon button. The app will close
entirely from the menu option, explained below.
Clicking SysTray icon use:
1) Click left and right in icon it shows the app
and you can interact with it. If you click again it toggles
show/hide the app.
2) With Middle Click a menu shows up, and dialog hides.
From this menu you can close the app entirely.
PROBLEM TO SOLVE:
When lose focus(eg:clicking outisde) he dialog should hide
Even if the dialog had a button to display a modal window
'''
class LauncherDialog(QDialog):
def __init__(self, x=None, y=None, parent=None):
super(LauncherDialog, self).__init__(parent)
my_lbl = QLabel(message)
my_layout = QVBoxLayout()
my_layout.addWidget(my_lbl)
my_layout.setMargin(0)
self.setLayout(my_layout)
self.setMinimumHeight(630)
self.setMinimumWidth(360)
self.setWindowTitle("Launcher")
if x is not None and y is not None:
self.move(x, y)
class SystrayLauncher(object):
def __init__(self):
# Create the icon
w = QWidget() #just to get the style(), haven't seen other way
icon = w.style().standardIcon(QStyle.SP_MessageBoxInformation)
# Create the tray
self.tray = QSystemTrayIcon()
self.tray.setIcon(icon)
self.tray.setVisible(True)
self.tray_pos = self.tray.geometry()
left = self.tray_pos.left()
height = self.tray_pos.height()
self.menu = QMenu()
self.action = QAction("&Quit", None, triggered=QApplication.instance().quit)
self.tray.setContextMenu(self.menu)
self.launcher_window = LauncherDialog(x=left, y=height)
self.tray.activated.connect(self.handleLeftRightMiddleClick)
def handleLeftRightMiddleClick(self, signal):
if signal == QSystemTrayIcon.ActivationReason.MiddleClick:
self.menu.addAction(self.action) #show menu
self.launcher_window.hide()
elif signal == QSystemTrayIcon.ActivationReason.Trigger: #left / right clicks
self.menu.removeAction(self.action) #hide menu
if self.launcher_window.isHidden():
self.launcher_window.show()
else:
self.launcher_window.hide()
else: #doubleclick
pass
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
sl = SystrayLauncher()
sys.exit(app.exec_())
This is a simplification of an application I wrote.
The application's main window has a button and a checkbox.
The checkbox resides inside a QScrollArea (via a widget).
The checkbox has a number stating how many times that checkbox was created.
Clicking on the button will open a dialog with a refresh button.
Clicking on the refresh button will set a new widget to the scroll area with a new checkbox.
The checkbox has a context menu that opens the same dialog.
However, when the widget is created using the dialog triggered by the context menu of the checkbox the application crashes with the following error:
2016-08-03 09:22:00.036 Python[17690:408202] modalSession has been
exited prematurely - check for a reentrant call to endModalSession:
Python(17690,0x7fff76dcb300) malloc: * error for object
0x7fff5fbfe2c0: pointer being freed was not allocated
* set a breakpoint in malloc_error_break to debug
The crash doesn't happen when clicking on the button to open the dialog and clicking refresh from the dialog.
The crash happens on both Mac and Windows.
I am using Python 2.7.10 with PySide 1.2.4
#!/usr/bin/env python
import sys
from itertools import count
from PySide import QtCore, QtGui
class MainWindow(QtGui.QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.centralwidget = QtGui.QWidget(parent=self)
self.open_diag_btn = QtGui.QPushButton('Open dialog', parent=self)
self.open_diag_btn.clicked.connect(self.open_dialog)
self.scroll_widget = QtGui.QScrollArea(parent=self)
layout = QtGui.QGridLayout(self.centralwidget)
layout.addWidget(self.scroll_widget)
layout.addWidget(self.open_diag_btn)
self.setCentralWidget(self.centralwidget)
self.set_scroll_widget()
def open_dialog(self):
dialog = Dialog(parent=self)
dialog.refresh.connect(self.set_scroll_widget) # Connecting the signal from the dialog to set a new widget to the scroll area
dialog.exec_()
# Even if I call the function here, after the dialog was closed instead of using the signal above the application crashes, but only via the checkbox
# self.set_scroll_widget()
def set_scroll_widget(self):
"""Replacing the widget of the scroll area with a new one.
The checkbox in the layout of the widget has an class instance counter so you can see how many times the checkbox was created."""
widget = QtGui.QWidget()
layout = QtGui.QVBoxLayout(widget)
widget.setLayout(layout)
open_diag_check = RefreshCheckbox(parent=self)
open_diag_check.do_open_dialog.connect(self.open_dialog) # Connecting the signal to open the dialog window
layout.addWidget(open_diag_check)
self.scroll_widget.setWidget(widget)
class RefreshCheckbox(QtGui.QCheckBox):
"""A checkbox class that has a context menu item which emits a signal that eventually opens a dialog window"""
do_open_dialog = QtCore.Signal()
_instance_counter = count(1)
def __init__(self, *args, **kwargs):
super(RefreshCheckbox, self).__init__(unicode(self._instance_counter.next()), *args, **kwargs)
self.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
action = QtGui.QAction(self)
action.setText("Open dialog")
action.triggered.connect(self.emit_open_dialog)
self.addAction(action)
def emit_open_dialog(self):
self.do_open_dialog.emit()
class Dialog(QtGui.QDialog):
"""A dialog window with a button that emits a refresh signal when clicked.
This signal is used to call MainWindow.set_scroll_widget()"""
refresh = QtCore.Signal()
def __init__(self, *args, **kwargs):
super(Dialog, self).__init__(*args, **kwargs)
self.refresh_btn = QtGui.QPushButton('Refresh')
self.refresh_btn.clicked.connect(self.do_refresh)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.refresh_btn)
def do_refresh(self):
self.refresh.emit()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
mySW = MainWindow()
mySW.show()
mySW.raise_()
app.exec_()
It looks like Python is trying to delete an object (or one of its child objects) which is no longer there - but quite what causes that to happen is not completely clear to me. The problematic object is the widget set on the scroll-area. If you excplicitly keep a reference to it:
def set_scroll_widget(self):
self._old_widget = self.scroll_widget.takeWidget()
...
your example will no longer dump core.
I think running the dialog with exec may somehow be the proximal cause of the problem, since this means it will run with its own event-loop (which might have an affect on the order of deletion-related events). I was able to find a better fix for your example by running the dialog with show:
def open_dialog(self):
dialog = Dialog(parent=self)
dialog.setAttribute(QtCore.Qt.WA_DeleteOnClose)
dialog.refresh.connect(self.set_scroll_widget)
dialog.setModal(True)
dialog.show()
Doing things this way means it's no longer necessary to keep an explicit reference to the previous scroll-area widget.
I am trying to open a new window once a button is clicked using Python PyQt library. Currently, I can create two buttons in my frame but I cannot click the first button where it should open a new window and hide my first window. There I have created a back button for it to go back to my first window. Can anyone help to teach me how to create a new window and using button click to open a new window.
This is my Python code:
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(300, 300, 600, 400)
self.setWindowTitle("Testing Window")
# self.setWindowIcon(QtGui.QIcon(''))
self.home()
def home(self):
btn = QtGui.QPushButton("QR Code", self)
btn.clicked.connect(self.qr)
btn.resize(100, 100)
btn.move(100, 100)
btn1 = QtGui.QPushButton("Face Recognition", self)
btn1.clicked.connect(QtCore.QCoreApplication.instance().quit)
btn1.resize(200, 100)
btn1.move(300, 100)
self.show()
def qr(self):
backbtn = QtGui.QPushButton("Back" , self)
backbtn.clicked.connect(self.home)
backbtn.resize(100, 100)
backbtn.move(100, 100)
self.show()
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
# GUI1 = QRCode()
sys.exit(app.exec_())
run()
There's only one window in your code, the one you instantiate with GUI=Window().
Your function qr does not create a new window, it:
creates a third button on the same window
try to show the window, which has no effect since the window is already shown. The third button doesn't appear because it's created after the first call to self.show()
To open a new window on a button click, see this answer. You can use hide() for the first window.
On a side note, for a basic GUI you don't need QMainWindow, a simple QWidget will work fine. Also, you might want to look at layouts instead of absolute positioning.
his has plagued me for eons, mostly due to how many combinations of methodologies there are for moving widgets and whatnot. Essentially I have a simple widget that I'd like to be able to pop up in specific areas of my app. Problem is I can never seem to get it to pop up where I want it. Additionally, I'd like to set it up in a way where I can adjust the "pointer" side of it based on whether it's popping up to point at a widget in the top-left of the app versus, say, the bottom-right.
Ideally, I'd be able to place the popup nearly adjacent to the edges of the parent widget, and anchor it based on where it is. Here's what I've been trying.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class popup(QWidget):
def __init__(self, parent = None, widget=None):
QWidget.__init__(self, parent)
layout = QGridLayout(self)
button = QPushButton("Very Interesting Text Popup. Here's an arrow ^")
layout.addWidget(button)
self.move(widget.rect().bottomLeft())
class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
self.button = QPushButton('Hit this button to show a popup', self)
self.button.clicked.connect(self.handleOpenDialog)
self.button.move(250, 50)
self.resize(600, 200)
def handleOpenDialog(self):
self.popup = popup(self, self.button)
self.popup.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_())
This code generates a button that's randomly in the middle of the widget. What I'm trying to get is, in this example, the popup to show under the button with its "pivot" in the top right such that the arrow in the popup button would be pointing to the bottom right corner of the widget. However it's popping up in the top left of the Window instead. In all of my messing around with .move, .setGeometry, and playing with QRect, I can't for the life of me figure this out. Huge kudos to whoever can lend a hand. Thanks!
I know this is old, but I was searching for this recently and this is the best answer; I have a useful addition (for anyone else searching for this recipe!)
I implemented it as a mixin, which I think gives more flexibility to your dialogs:
class PopupDialogMixin(object): # will not work (with PySide at least) unless implemented as 'new style' class. I.e inherit from object
def makePopup(callWidget):
"""
Turns the dialog into a popup dialog.
callWidget is the widget responsible for calling the dialog (e.g. a toolbar button)
"""
self.setContentsMargins(0,0,0,0)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.Popup)
self.setObjectName('ImportDialog')
# Move the dialog to the widget that called it
point = callWidget.rect().bottomRight()
global_point = callWidget.mapToGlobal(point)
self.move(global_point - QtCore.QPoint(self.width(), 0))
Your custom dialog would then inherit from both QtCore.QDialog and PopupDialogMixin. This gives you the option to use your dialog in the 'normal' way or make it a popup dialog. e.g:
dlg = MyDialog(self)
dlg.makePopup(self.myButton)
I think implementing it as a mixin gives a number of benefits:
No need to write the 'popup' code for each custom dialog you want as a popup
'Default' behaviour of the dialog is preserved - e.g. if you want to reuse it somewhere else as a 'regular' dialog, you just use it like normal
No need to pass anything extra to __init__ other than parent.
Here you go - the comments kind of explain the logic behind it - since the question is an example and about the positioning, I kept the rest of the code the same except the popup class, but just to mention cause its a pet peeve - you shouldn't import * (ever) but especially with something as big as PyQt4.QtCore/QtGui...
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class popup(QWidget):
def __init__(self, parent = None, widget=None):
QWidget.__init__(self, parent)
layout = QGridLayout(self)
button = QPushButton("Very Interesting Text Popup. Here's an arrow ^")
layout.addWidget(button)
# adjust the margins or you will get an invisible, unintended border
layout.setContentsMargins(0, 0, 0, 0)
# need to set the layout
self.setLayout(layout)
self.adjustSize()
# tag this widget as a popup
self.setWindowFlags(Qt.Popup)
# calculate the botoom right point from the parents rectangle
point = widget.rect().bottomRight()
# map that point as a global position
global_point = widget.mapToGlobal(point)
# by default, a widget will be placed from its top-left corner, so
# we need to move it to the left based on the widgets width
self.move(global_point - QPoint(self.width(), 0))
class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
self.button = QPushButton('Hit this button to show a popup', self)
self.button.clicked.connect(self.handleOpenDialog)
self.button.move(250, 50)
self.resize(600, 200)
def handleOpenDialog(self):
self.popup = popup(self, self.button)
self.popup.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Window()
win.show()
sys.exit(app.exec_())