Retrieving pen pressure with Python 2.7 via PySide - python

I'm using Python 2.7 with PySide and want to use this to retrieve pen pressure from my wacom pen.
So I tried setting up the following
import PySide
from PySide import QtCore, QtGui
pressure = PySide.QtGui.QTabletEvent.pressure()
print pressure
That threw the following error
pressure = PySide.QtGui.QTabletEvent.pressure()
TypeError: descriptor 'pressure' of 'PySide.QtGui.QTabletEvent' object needs an argument
The object it needs is a "PySide.QtGui.QTabletEvent" Object. But I have no idea how can I retrieve such an object.
So my question is, how do I retrieve the wacom penpressure using QTabletEvent?

You need to receive the actual event and obtain the pressure from it.
Example:
import sys
from PySide import QtCore, QtGui
class MyWidget(QtGui.QWidget):
def tabletEvent(self, e):
print(e.pressure())
app = QtGui.QApplication(sys.argv)
widget = MyWidget()
widget.show()
app.exec_()

Related

QwebEngineView doesn't show anything

I have created a piece of code that show a simple webpage (a bokeh graph saved in an html file). This code is working in Windows 10 at work, but on macos-mojave, using PyQT5.9, with Python 3.6, the opened window don't show anything, and makes python crash.
Could anyone helps ?
Many thanks
import sys
from PyQt5.QtWidgets import (QApplication)
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5 import QtCore
class Principale():
def __init__(self):
self.view = QWebEngineView()
self.view.load(QtCore.QUrl("/Users/moncompte/Desktop/essai.html"))
self.view.show()
# Create a custom font
# ---------------------
app = QApplication(sys.argv)
fenetre=Principale()
sys.exit(app.exec_())

pyqt5, Receiving AttributeError: 'QMainWindow' object has no attribute 'browseSlot'

I'm learning pyqt5, and specifically how to use it with the QT Designer. I'm sort of following the turorial HERE. However in this tutorial they are converting the XML interface to Python code with pyuic5, while I'm trying to import it dynamically with uic.loadUi("myui.ui"). In the tutorial we define a slot with the signals and slot editor named " browseSlot".
When I try to run/compile, at the line
dlg = uic.loadUi("myui.ui")
I get the error:
AttributeError: 'QMainWindow' object has no attribute 'browseSlot'
I think what's going on is that QT Designer connects a signal to the slot 'browseSlot' but because a 'browseSlot' method isn't defined in the myui.ui, the error is thrown, because there is no way for the interpreter to know I'm referring to a method that is outside the UI interface file. (In this case, in the module that loads the interface). As far as I can tell QT Designer only lets me connect signals to slots, not define a whole new one. I think that way this is handled in other frameworks is that there will be an abstract method that needs over riding. So what can I do in this situation to make it work?
from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtCore import QObject, pyqtSlot
import sys
app = QtWidgets.QApplication([])
dlg = uic.loadUi("myui.ui")
#pyqtSlot
def returnPressedSlot():
pass
#pyqtSlot
def writeDocSlot():
pass
#pyQt
def browseSlot():
pass
dlg.show()
sys.exit(app.exec())
The slots belong to the class that is used returns loadUi(), they are not any functions since they do not magically not connect them, if you want to use loadUi() and implement these methods you must inherit from the class corresponding to the template that you used, in the example of the link Main Window was used so it must be inherited from QMainWindow:
from PyQt5 import QtCore, QtGui, QtWidgets, uic
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
uic.loadUi("mainwindow.ui", self)
#QtCore.pyqtSlot()
def returnPressedSlot():
pass
#QtCore.pyqtSlot()
def writeDocSlot():
pass
#QtCore.pyqtSlot()
def browseSlot():
pass
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
try this out
from PyQt5 import QtWidgets, uic
app = QtWidgets.QApplication([])
form = uic.loadUi("login.ui")
form2.show()
app.exec()
the above python code should display your gui app properly as long as you have install PyQt5 and PyQt5-tools,if you haven't then open CMD and typeenter code here "pip install PyQt5" and click enter.once installation is done type "pip install PyQt5-tools" then you are good to go

How can I self hide and show QDialog() in PyQT5?

I have a GUI that was generated using Qt Designer, I used pyuic5 to generate a .py file. In a separate py (program.py) file I import my UI a do all my work there.
program.py
import sys, os, time
from subprocess import call
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyCred_GUI import Ui_Dialog
class MyGUI(Ui_Dialog):
def __init__(self, dialog):
Ui_Dialog.__init__(self)
self.setupUi(dialog)
self.pushButton_2.clicked.connect(self.cancelbutton)
def cancelbutton(self):
exit()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
dialog = QtWidgets.QDialog()
dialog.setWindowFlags(QtCore.Qt.WindowSystemMenuHint)
prog = MyGUI(dialog)
dialog.show()
sys.exit(app.exec_())
I pulled a lot out just to focus on the issue here. When I click my Cancel button, I want the window to hide, set a timer, and then reappear after so many seconds. I have tried every combination of self.close() self.hide() self.destroy() and none of them hide my window. I get an error that says
"AttributeError: 'MyGUI' object has no attribute 'hide'"
Which makes sense because MyGUI doesn't have a hide() function. I am at a complete loss on how to hide this window.
EDIT (Solved)
For future people, as suggested by Hi Im Frogatto dialog.hide() worked.
In your code snippet, dialog is of type QDialog and thereby having hide method. However instances of MyGUI class seem to not have such a method. So, if you write dialog.hide() in that __init__() function, you can hide it.

'module' object has no attribute 'qmainwindow'

I am trying to create UI using PyQt4 on centos. But When ever I am trying to load QtGui.QMainWindow I am getting the error:
Traceback(most recent call last):
File "test.py", line 7, in <module>
Ui_MainWindow, QtBaseClass = uic.loadUiType(ui_file_path)
File "/usr/local/lib/python2.7/site-packages/PyQt4/uic/__init__.py", line 160, inloadUiType
return (ui_globals[winfo["uiclass"]],getattr(Qtgui, winfo["baseclass"]))
AttributeError:'module' object has no attribute 'qmainwindow'
This My Code,I am using python 2.7.9:
import sys
import os
from PyQt4 import QtCore, QtGui, uic
ui_file_path = os.getcwd()+"/test.ui" # Enter file here.
Ui_MainWindow, QtBaseClass = uic.loadUiType(ui_file_path)
class MyApp(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
Thanks in advance.
I was having the same issue on Ubuntu 16.04, using PyQt5, and python3. This could apply to PyQt4 as well, so why not give it a shot?
I found from https://doc.bccnsoft.com/docs/PyQt5/pyqt4_differences.html# the solution to be changing QtGui.QMainWindow to QtWidget.QMainWindow as they have changed. This also needed to be applied to QApplication as well, and the import of QtGui was not needed in the code (replaced with import ..., QtWidget).
I wish you luck in solving this issue.
Forgive me if I miss the point, but I don't understand why MyApp inherits from both QtGui.QMainWindow and Ui_MainWindow.
Would something like this work for you?
import sys
import os
from PyQt4 import QtGui, uic
ui_file_path = os.path.join(os.getcwd(), "test.ui") # Enter file here.
class MyApp(QtGui.QMainWindow):
def __init__(self):
# Parent class init
QtGui.QMainWindow.__init__(self)
# You may also write
# super(MyApp, self).__init__()
# Load UI
uic.loadUi(ui_file_path, self)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
Edit
There seems to be a case issue, here.
In my Python interpreter, I get this:
import os
os.PAth
# This prints: AttributeError: 'module' object has no attribute 'PAth'
os.path
# This works
In your case, you enter the correct case, but get an error with the attribute in an incorrect case in the error message:
class Myapp(QtGui.QMainWindow)
# AttributeError:'module' object has no attribute 'qmainwindow'
As if somewhere between the text editor and the interpreter, something had lowered the case of QMainWindow. I don't have any hypothesis regarding why this would happen, but it could be nested somewhere not obvious.
Can you reproduce in an interpreter? More explicitly, can you run a Python interpreter on the same machine and try this:
import PyQt4.QtGui
PyQt4.QtGui.QMainWindow
import os
os.PAth
and see what errors you get?
(Note: in your comment below, there's a case error in QtGui.QMainWindow (you wrote QtGui.QMainWIndow). I suppose you made it while writing the comment, not in the code.)

How to get maya main window pointer using PySide?

I've used PyQt4 in maya quite a bit, and generally I've found it quite easy to switch to PySide, but I'm having trouble getting the pointer to the main window. Perhaps someone can understand what's going wrong.
Here's what I do in PyQt4:
import sip, PyQt4.QtCore
import maya.OpenMayaUI as mui
ptr = mui.MQtUtil.mainWindow()
parent = sip.wrapinstance(long(ptr), PyQt4.QtCore.QObject)
This works fine. When I try the same in PySide:
import sip, PySide.QtCore
import maya.OpenMayaUI as mui
ptr = mui.MQtUtil.mainWindow()
parent = sip.wrapinstance(long(ptr), PySide.QtCore.QObject)
I get the following error:
# Error: wrapinstance() argument 2 must be sip.wrappertype, not Shiboken.ObjectType
# Traceback (most recent call last):
# File "<maya console>", line 4, in <module>
# TypeError: wrapinstance() argument 2 must be sip.wrappertype, not Shiboken.ObjectType #
Anyone know what's going wrong?
You need to import shiboken instead of sip and pass QWidget to wrapInstance instead of QObject
Edit: Maya2017 contains shiboken2 and PySide2 instead of shiboken and PySide as pointed out in comments below.
import shiboken
from PySide import QtGui, QtCore
import maya.OpenMayaUI as apiUI
def getMayaWindow():
"""
Get the main Maya window as a QtGui.QMainWindow instance
#return: QtGui.QMainWindow instance of the top level Maya windows
"""
ptr = apiUI.MQtUtil.mainWindow()
if ptr is not None:
return shiboken.wrapInstance(long(ptr), QtGui.QWidget)
Please note that sip has wrapinstance in which i is not capital but in shiboken.wrapInstance i is capital.
shiboken.wrapInstance() requires wrapertype as a second argument, so you can pass QWidget as a second argument.
Because the previous answer has become somewhat out-of-date, here is a more current version to save someone the trouble of having to update it themselves.
Two approaches for getting the maya main window:
using PySide2:
from PySide2 import QtWidgets
global app
app = QtWidgets.QApplication.instance() #get the qApp instance if it exists.
if not app:
app = QtWidgets.QApplication(sys.argv)
def getMayaMainWindow():
mayaWin = next(w for w in app.topLevelWidgets() if w.objectName()=='MayaWindow')
return mayaWin
print(getMayaMainWindow())
Using shiboken2 and the maya api:
import maya.OpenMayaUI as apiUI
from PySide2 import QtWidgets
try:
import shiboken2
except:
from PySide2 import shiboken2
def getMayaMainWindow():
ptr = apiUI.MQtUtil.mainWindow()
mayaWin = shiboken2.wrapInstance(long(ptr), QtWidgets.QWidget)
return mayaWin
print(getMayaMainWindow())
depending on your needs, the following more modern code might be easier too use.
since maya now has a build in method to access the main window in qt
import pymel.core as pm
mayaWindow = pm.ui.Window("MayaWindow").asQtObject()
your_widget.setParent(mayaWindow)

Categories

Resources