PyQt QTest click on QMenu - pyqt

In my PyQt app I have a main menu, which has the following structure:
Main Menu
|___
|
Language
| |
| Russian
| |
| English
Exit
I want to perform a click on Russian to test if the language is changed. I have a link to language_menu in GUI
[...somewhere in MainWindow...]
self.language_menu = QtGui.QMenu()
[....]
so
from PyQt4 import QtGui, QtCore
from PyQt4.QTest import QTest
from gui import MainWindow
class TestMainWindow(unittest.TestCase):
def setUp(self):
self.app = QtGui.QApplication([])
self.ui = MainWindow()
def tearDown(self):
self.app.deleteLater()
def test_translation(self):
menu = self.ui.language_menu
rus_lang = menu.actions()[0]
QTest.mouseClick(rus_lang, QtCore.Qt.LeftButton)
but it tells me that
argument 1 has unexpected type 'QAction'
How do I do this? Is it even possible?

You can use rus_lang.trigger() or rus_lang.toggle() to activate the menu item.

i think ekhumoro's answer is wrong because it misses the point of the test,
rus_lang.trigger() isnt interacting with the gui.
for me QTest.mouseClick(rus_lang, QtCore.Qt.LeftButton)
works fine with QButton maybe QAction has diffrent API.

Related

Error Cannot create instance of abstract (non-instantiable) type `GtkBox' python3

I have error when run this code:
from gi.repository import Gtk
class MainWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="")
# box
self.box = Gtk.Box(spacing=10)
self.add(self.box)
# bacon button
self.bacon_button = Gtk.Button(label="Bacon")
self.bacon_button.connect("clicked", self.bacon_clicked)
self.box.pack_start(self.bacon_button, True, True,0)
# tuna button
self.tuna_button = Gtk.Button(label="Tuna")
self.tuna_button.connect("clicked", self.tuna_clicked)
self.box.pack_start(self.tuna_button, True, True,0)
def bacon_clicked(self, widget):
print("You clicked Bacon")
def tuna_clicked(self, widget):
print("You clicked Tuna")
window = MainWindow()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()
And error at output:
File "/Users/*********/Documents/Program/Tutorial/venv/lib/python3.11/site-packages/gi/overrides/__init__.py", line 319, in new_init
return super_init_func(self, **new_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: cannot create instance of abstract (non-instantiable) type `GtkBox'
Process finished with exit code 1
I currently using python3 and already install PyGObject package in PyCharm
Your code is loading GTK2, instead of GTK3; in GTK 2.x, GtkBox is indeed an abstract type.
If you want to use GTK 3.x, you will need to add:
import gi
gi.require_version('Gtk', '3.0')
at the top, before importing the Gtk namespace.
Don't use Box directly. If you want a Box that grows horizontally with pack_start, use Gtk.HBox (documented here).
If you want it to grow vertically, use Gtk.VBox (documented here).
That being said, the more modern (GTK4) way of doing things is to use a Gtk.Grid for everything.

Text of label in UI file can't be changed

I tried it.
from PySide2 import QtWidgets
from PySide2 import QtGui
from PySide2 import QtCore
from PySide2.QtUiTools import QUiLoader
from maya.app.general.mayaMixin import MayaQWidgetBaseMixin
import shiboken2 as shiboken
import os
UIFILEPATH = 'D:/MAYA/pyside_pick/ui/PicsTest5.ui'
class MainWindow(MayaQWidgetBaseMixin,QtWidgets.QMainWindow):
def __init__(self,parent=None):
super(MainWindow,self).__init__(parent)
self.UI = QUiLoader().load(UIFILEPATH)
self.setWindowTitle(self.UI.windowTitle())
self.setCentralWidget(self.UI)
#image
img = QtGui.QPixmap('D:/MAYA/pyside_pick/images/imgKohaku.png')
self.scene = QtWidgets.QGraphicsScene(self)
item = QtWidgets.QGraphicsPixmapItem(img)
self.scene.addItem(item)
self.UI.graphicsView_char_1.setScene(self.scene)
#filter
self._filter = Filter()
self.installEventFilter(self._filter)
self.UI.pSphere1.installEventFilter(self._filter)
#primary
self.UI.label.setStyleSheet("QLabel {color : white;}")
self.UI.label.setText("A")
def labelTest(self):
self.UI.label.setStyleSheet("QLabel {color : red;}")
self.UI.label.setText("B")
print('D')
return False
class Filter(QtCore.QObject):
def eventFilter(self, widget, event):
win = MainWindow()
if event.type() == QtCore.QEvent.MouseButtonPress:
print(widget.objectName())
cmds.select(widget.objectName())
win.labelTest()
return False
def main():
win = MainWindow()
win.show()
if __name__ == '__main__':
main()
I clicked the button that 'pSphere1', but
self.UI.label.setStyleSheet("QLabel {color : red;}") self.UI.label.setText("B")
were look like it's not working.
I can change it inside define with UI loaded, but can't I do setText from outside?
How can I change the label of an imported UI file?
I find this, but I really do not understand. I couldn't find any mention of them beyond this page.
Change comboBox values in Qt .ui file with PySide2
If you know, I also want you to tell me where to put them.
Your issue is within the eventFilter(), and specifically the first line:
win = MainWindow()
This will create a new main window instance, which clearly doesn't make sense, since you obviously want to interact with the existing one.
While you could add the instance as an argument in the filter constructor in order to get a reference to the instance and directly call the function, that wouldn't be very good from the OOP point of view, as objects should never directly access attributes of their "parents".
A better and more correct approach would be to use a custom signal instead, and connect it from the main window instance:
class Filter(QtCore.QObject):
testSignal = QtCore.Signal()
def eventFilter(self, widget, event):
if event.type() == QtCore.QEvent.MouseButtonPress:
print(widget.objectName())
cmds.select(widget.objectName())
self.testSignal.emit()
return False
class MainWindow(MayaQWidgetBaseMixin, QtWidgets.QMainWindow):
def __init__(self, parent=None):
# ...
self._filter.testSignal.connect(self.labelTest)
Note that widgets could accept events and prevent propagation (for instance, buttons or graphics views that have selectable or movable items), so you might not receive the event in the filter in those cases.

Change WM_CLASS of Dialog (tkinter)

Is there a way to change WM_CLASS of showinfo (and other dialogs)? className, class_ parameters do the job for Tk, Toplevel.
import tkinter as tk
from tkinter.messagebox import showinfo
if __name__ == '__main__':
root = tk.Tk(className='ymail')
mail_client = tk.Toplevel(root, class_='ymail')
new_message = tk.Toplevel(root, class_='ymail')
showinfo(title="Cancel sending", parent=new_message, message="""
Send is cancelled due to empty message""")
root.mainloop()
For showinfo dialog
$ xprop WM_CLASS
gives
WM_CLASS(STRING) = "__tk__messagebox", "Dialog"
I think it is convenient to cycle tkinter windows with Alt-~ (Tilde), for which their WM_CLASS shall be the same.
I did the search ("tkinter change WM_CLASS showinfo"). Some of the hits are not applicable, some don't work (xdotool), and some I'd rather use as a last resort (converting C program to python).
Using
Debian 10
python 3.7.3
GNOME 3.30.1
EDIT
Added workaround (using xdotool)
import threading
import subprocess
import time
import tkinter as tk
from tkinter.messagebox import showinfo
def change_dialog_class(from_="Dialog", to_="ymail"):
cmd = f"xdotool search --class {from_} set_window --class {to_}"
time.sleep(1)
subprocess.run(cmd.split())
if __name__ == '__main__':
root = tk.Tk(className='ymail')
mail_client = tk.Toplevel(root, class_='ymail')
new_message = tk.Toplevel(root, class_='ymail')
tk.Frame.class_ = 'ymail'
threading.Thread(target=change_dialog_class, args=("Dialog", "ymail"),
daemon=True).start()
showinfo(title="Cancel sending", parent=new_message,
message="""Send is cancelled due to empty message""")
root.mainloop()
along with ymail.desktop it works
$ cat ~/.local/share/applications/ymail.desktop
[Desktop Entry]
Type=Application
Terminal=false
Name=ymail
Icon=python
StartupWMClass=ymail
yet, the python solution would be better
Since I'm not a XSystem user it took me some time to follow up. It
seems like that you are looking for wm_group and unfortunately it isnt
possible without subclassing it, which results in pretty much the same
as writing your own class with tk.Toplevel. Anyway I hope
toplevel.wm_group(root) ease things out and works for you.
After I noticed that the SimpleDialog may has some functionality that you want to keep and can be hard to code for yourself, I decided to write an answer that you may want to use. It also provides the class_ option in case wm_group dosent work for you.
Here is the code:
import tkinter as tk
import tkinter.simpledialog as simpledialog
class MessageBox(simpledialog.SimpleDialog):
def __init__(self, master,**kwargs):
simpledialog.SimpleDialog.__init__(self,master,**kwargs)
#root.tk.call('wm', 'group', self.root._w, master)
def done(self,num):
print(num)
self.root.destroy()
root = tk.Tk()
MessageBox(root,title='Cancel',text='Im telling you!',class_='ymail',
buttons=['Got it!','Nah'], default=None, cancel=None)
root.mainloop()
and here is the source:
https://github.com/python/cpython/blob/main/Lib/tkinter/simpledialog.py#L31

How to avoid parent widget warning when using QFileDialog class?

I'm studying Python and PyQt5 in Microsoft Windows 7. My IDE is PyCharm 4.5 CE.
I am trying to make the file dialog to users can select the files or directories easily.
My code is...
# coding: utf-8
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.init_gui()
def init_gui(self):
file_names = QFileDialog.getOpenFileNames(self, "Select one or more files to open", "C:/Windows", "")
print(file_names)
self.setGeometry(100, 100, 500, 300)
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
mw = MainWindow()
sys.exit(app.exec_())
This code works properly. But the only thing that annoying me is this.
There are many buttons in parent main window and one of the button shows file dialog.
What is the proper parent in this situation?
From PyQt5 documentation the method signature is:
QStringList getOpenFileNames (QWidget parent=None, QString caption=QString(), QString directory=QString(), QString filter=QString(), QString selectedFilter=None, Options options=0)
The parent must be an instance of QWidget or of some class that inherits from QWidget, and that's exactly what QMainWindow is (and this explains why everything works as expected).
Now, to understand why PyCharm displays a warning: if you look in the QFileDialog.py file, which is automatically generated by PyCharm from PyQt5\QtWidgets.pyd you will see that the method getOpenFileNames is not declared as staticmethod nor as classmethod:
def getOpenFileNames(self, QWidget_parent=None, str_caption='', str_directory='', str_filter='', str_initialFilter='', QFileDialog_Options_options=0): # real signature unknown; restored from __doc__
""" QFileDialog.getOpenFileNames(QWidget parent=None, str caption='', str directory='', str filter='', str initialFilter='', QFileDialog.Options options=0) -> (list-of-str, str) """
pass
so PyCharm expects (wrongly) the method to be called on an instance of QFileDialog, but here you have no instance of QFileDialog (as the method docstring suggests the real method signature is unknown), hence it expects the first argument of the method (self) to be an instance of QFileDialog and thus it throws a warning.
You can turn off such warning by disabling the inspection just for the desired statement:
# noinspection PyTypeChecker,PyCallByClass
file_names = QFileDialog.getOpenFileNames(self, "Select one or more files to open", "C:/Windows", "")
print(file_names)

Incorrect behaviour of print() when executed from within a QTDialog window in Spyder

I am working on a very simple interface to explore/graph csv files. My aim is ultimately to explore, not to build software as I am not a developer, more of a "desperate user" :-)
I am leveraging the code found in this example
These are my first steps both in Python and in GUI, so I tend to put print messages in my calls so that I can more or less track what is happening. And this is where I found a strange behavior if I run the code from within Spyder.
import sys
import os
from PyQt4 import QtGui
import pandas as pd
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
# QtGui.QDialog
class Window(QtGui.QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
# a figure instance to plot on
self.figure = plt.figure()
# this is the Canvas Widget that displays the `figure`
# it takes the `figure` instance as a parameter to __init__
self.canvas = FigureCanvas(self.figure)
# this is the Navigation widget
# it takes the Canvas widget and a parent
self.toolbar = NavigationToolbar(self.canvas, self)
# Just some extra button to mess around
self.button= QtGui.QPushButton('Push Me')
self.button.clicked.connect(self.do_print)
# set the layout
layout = QtGui.QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
layout.addWidget(self.button)
self.setLayout(layout)
def do_print(self):
print('Hello World!!')
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = Window()
main.show()
sys.exit(app.exec_())
The strange behavior is that if I push the button once, nothing happens on the Ipython console. By the second time I push, then two "Hello World!" printouts appear.
If, on the other hand, I just launch my script from within a Windows Shell:
python my_simple_test.py
Then everything works as expected.
What am I then doing wrong from within Spyder?
Thanks,
Michele
IPython buffers stdout a bit differently from a terminal. When something is printed, it looks at how long it has been since it last flushed the buffer, and if it's longer than some threshold, it flushes it again. So the second time you click the button, it flushes stdout, and you see both outputs.
You can force it to flush immediately like this:
print('Hello World!!')
sys.stdout.flush()

Resources