Im trying to compile this Python3.3-Code provided by:
http://www.youtube.com/watch?v=fqK8N48kPXs&feature=c4-overview-vl&list=PLA955A8F9A95378CE
I have installed QT 4.8.4 VS2012, PySide 1.1.2, python 3.3, Qt 5
import sys
from PySide.QtCore import *
from PySide.QtGui import *
import time
app = QApplication(sys.argv)
try:
#Default Values
due = QTime.currentTime()
message = "Alert!"
if len(sys.argv) < 2:
raise ValueError
hours, minutes = sys.argv[1].split(":")
due = QTime(int(hours), int(minutes))
if not due.isValid():
raise ValueError
if len(sys.argv) > 2:
message = " ".join(sys.argv[2:])
#python firstTutorial.py 15:36 This is my message
except ValueError:
message = "Usage: firstTutorial.py HH:MM [optional message]" #24 hour clock
while QTime.currentTime() < due:
time.sleep(10)
label = QLabel("<font color=red size=72><b>" + message + "</b></font>")
label.setWindowFlags(Qt.SplashScreen) #Keine Fenstereigenschaften nutzen
label.show()
QTimer.singleShot(20000, app.quit) #20 seconds till quit SIGNAL/SLOT
app.exec_()
But I get a Python Appcrash, after I give the parameter like: "15:23"
Problemsignatur: Problemereignisname: APPCRASH
Anwendungsname: python.exe Anwendungsversion: 0.0.0.0
Anwendungszeitstempel: 5194070d Fehlermodulname: QtCore4.dll
Fehlermodulversion: 4.8.4.0 Fehlermodulzeitstempel: 50eb4eec
Ausnahmecode: c0000005 Ausnahmeoffset: 0000000000033810
Betriebsystemversion: 6.2.9200.2.0.0.256.49 Gebietsschema-ID: 1031
Zusatzinformation 1: c436 Zusatzinformation
2: c436a4fc38414d2d1792ffc3e15f3c19 Zusatzinformation 3: 664a
Zusatzinformation 4: 664a8423adc2000b0b283f71335a059d
Reinstalled QT, Python and PySide, but nothing worked.
Any suggestions?
Related
how can I execute mouse clicks in windows from Python code that is running in WSL?
I tried using PyAutoGUI, however, I get the following error:
...
File "/usr/lib/python3.6/os.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'DISPLAY'
Because I am using wsl, it decides that my platform is linux. If I hardcode it to windows, ctypes do have attribute win.dll in dc = ctypes.windll.user32.GetDC(0)
if sys.platform == 'win32':
import ctypes
if _PILLOW_INSTALLED:
from PIL import ImageGrab
# Makes this process aware of monitor scaling so the screenshots are correctly sized:
try:
ctypes.windll.user32.SetProcessDPIAware()
except AttributeError:
pass # Windows XP doesn't support this, so just do nothing.
dc = ctypes.windll.user32.GetDC(0)
class POINT(ctypes.Structure):
_fields_ = [('x', ctypes.c_long),
('y', ctypes.c_long)]
def _winPosition():
cursor = POINT()
ctypes.windll.user32.GetCursorPos(ctypes.byref(cursor))
return (cursor.x, cursor.y)
position = _winPosition
def _winScreenshot(filename=None):
# TODO - Use the winapi to get a screenshot, and compare performance with ImageGrab.grab()
# https://stackoverflow.com/a/3586280/1893164
try:
im = ImageGrab.grab()
if filename is not None:
im.save(filename)
except NameError:
raise ImportError('Pillow module must be installed to use screenshot functions on Windows.')
return im
screenshot = _winScreenshot
def _winSize():
return (ctypes.windll.user32.GetSystemMetrics(0), ctypes.windll.user32.GetSystemMetrics(1))
size = _winSize
def _winGetPixel(x, y):
colorRef = ctypes.windll.gdi32.GetPixel(dc, x, y) # A COLORREF value as 0x00bbggrr. See https://learn.microsoft.com/en-us/windows/win32/gdi/colorref
red = colorRef % 256
colorRef //= 256
green = colorRef % 256
colorRef //= 256
blue = colorRef
return (red, green, blue)
getPixel = _winGetPixel
elif platform.system() == 'Linux':
from Xlib.display import Display
import errno
scrotExists = False
try:
whichProc = subprocess.Popen(
['which', 'scrot'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
scrotExists = whichProc.wait() == 0
except OSError as ex:
if ex.errno == errno.ENOENT:
# if there is no "which" program to find scrot, then assume there
# is no scrot.
pass
else:
raise
_display = Display(os.environ['DISPLAY'])
def _linuxPosition():
coord = _display.screen().root.query_pointer()._data
return coord["root_x"], coord["root_y"]
position = _linuxPosition
def _linuxScreenshot(filename=None):
if not scrotExists:
raise NotImplementedError('"scrot" must be installed to use screenshot functions in Linux. Run: sudo apt-get install scrot')
if filename is not None:
tmpFilename = filename
else:
tmpFilename = '.screenshot%s.png' % (datetime.datetime.now().strftime('%Y-%m%d_%H-%M-%S-%f'))
if scrotExists:
subprocess.call(['scrot', '-z', tmpFilename])
im = Image.open(tmpFilename)
# force loading before unlinking, Image.open() is lazy
im.load()
if filename is None:
os.unlink(tmpFilename)
return im
else:
raise Exception('The scrot program must be installed to take a screenshot with PyScreeze on Linux. Run: sudo apt-get install scrot')
screenshot = _linuxScreenshot
def _linuxSize():
return _display.screen().width_in_pixels, _display.screen().height_in_pixels
size = _linuxSize
def _linuxGetPixel(x, y):
rgbValue = screenshot().getpixel((x, y))
return rgbValue[0], rgbValue[1], rgbValue[2]
getPixel = _linuxGetPixel
Does anyone know how to solve this problem?
Just to clarify... pyautogui will not work with WSL? Based on what I have read elsewhere this has to do with the lack of an X-server in the linux subsystem. Similar to how pyautogui will not work over a remote or headless terminal session.
So, try this with just plain old windows and it will work!
you could try adding this line AFTER importing pyautogui.
import pyautogui._pyautogui_win as platformModule
This should reassign / override the platformModule variable to the Windows version..
(In theory)
I am trying to write the Python script which prints the title of the active window using python in Mac OS.
Here is my code:
from AppKit import NSWorkspace
active_app_name = NSWorkspace.sharedWorkspace().frontmostApplication().localizedName()
print active_app_name
This code just prints name of the app like Google chrome or firefox, but not title. How to get title of the window?
Here is what I used to find both the active application name and window title on Mac OS X using Python by using Quartz API.
First of all, we need to add imports as required:
if sys.platform == "darwin":
import applescript
from AppKit import NSWorkspace
from Quartz import (
CGWindowListCopyWindowInfo,
kCGWindowListOptionOnScreenOnly,
kCGNullWindowID
)
And then we can get the active app name and window title via the code below:
def getActiveInfo(event_window_num):
try:
if sys.platform == "darwin":
app = NSWorkspace.sharedWorkspace().frontmostApplication()
active_app_name = app.localizedName()
options = kCGWindowListOptionOnScreenOnly
windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID)
windowTitle = 'Unknown'
for window in windowList:
windowNumber = window['kCGWindowNumber']
ownerName = window['kCGWindowOwnerName']
# geometry = window['kCGWindowBounds']
windowTitle = window.get('kCGWindowName', u'Unknown')
if windowTitle and (
event_window_num == windowNumber
or ownerName == active_app_name
):
# log.debug(
# 'ownerName=%s, windowName=%s, x=%s, y=%s, '
# 'width=%s, height=%s'
# % (window['kCGWindowOwnerName'],
# window.get('kCGWindowName', u'Unknown'),
# geometry['X'],
# geometry['Y'],
# geometry['Width'],
# geometry['Height']))
break
return _review_active_info(active_app_name, windowTitle)
if sys.platform == "win32":
(active_app_name, windowTitle) = _getActiveInfo_Win32()
return _review_active_info(active_app_name, windowTitle)
except:
log.error('Unexpected error: %s' % sys.exc_info()[0])
log.error('error line number: %s' % sys.exc_traceback.tb_lineno)
return 'Unknown', 'Unknown'
There is no access to app title from NSWorkspace.sharedWorkspace().activeApplication().
But you can find the current window title by its PID:
For example:
from AppKit import NSWorkspace
pid = NSWorkspace.sharedWorkspace().activeApplication()['NSApplicationProcessIdentifier']
Then find the right window using below code (it's stored in kCGWindowOwnerPID) as shown in below code:
Here is a complete shell example based on #JakeW's script:
#!/usr/bin/python
# Prints list of windows in the current workspace.
import sys
if sys.platform == "darwin":
from AppKit import NSWorkspace
from Quartz import (
CGWindowListCopyWindowInfo,
kCGWindowListOptionOnScreenOnly,
kCGNullWindowID
)
if sys.platform == "darwin":
curr_app = NSWorkspace.sharedWorkspace().frontmostApplication()
curr_pid = NSWorkspace.sharedWorkspace().activeApplication()['NSApplicationProcessIdentifier']
curr_app_name = curr_app.localizedName()
options = kCGWindowListOptionOnScreenOnly
windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID)
for window in windowList:
pid = window['kCGWindowOwnerPID']
windowNumber = window['kCGWindowNumber']
ownerName = window['kCGWindowOwnerName']
geometry = window['kCGWindowBounds']
windowTitle = window.get('kCGWindowName', u'Unknown')
if curr_pid == pid:
print("%s - %s (PID: %d, WID: %d): %s" % (ownerName, windowTitle.encode('ascii','ignore'), pid, windowNumber, geometry))
elif sys.platform == "win32":
(active_app_name, windowTitle) = _getActiveInfo_Win32()
It will list details of the current active window including its title.
As of macOS 10.6 and later it is better to use: frontmostApplication and if you want to get all of the applications listed you can call runningApplications method.
You can see more details at https://developer.apple.com/documentation/appkit/nsworkspace#overview
For example:
from AppKit import NSWorkspace
NSWorkspace.sharedWorkspace().runningApplications() // for getting all applications
NSWorkspace.sharedWorkspace().frontmostApplication() // for active window
I tried #kenorb's code but unlucky it can only get the app name, but no title content.
I finally found a way to do this with AppleScript:
You can find the answer here:
MacOSX: get foremost window title
First create a appleScript GetNameAndTitleOfActiveWindow.scpt
global frontApp, frontAppName, windowTitle
set windowTitle to ""
tell application "System Events"
set frontApp to first application process whose frontmost is true
set frontAppName to name of frontApp
tell process frontAppName
tell (1st window whose value of attribute "AXMain" is true)
set windowTitle to value of attribute "AXTitle"
end tell
end tell
end tell
return {frontAppName, windowTitle}
You can test it first in your mac terminal:
osascript GetNameAndTitleOfActiveWindow.scpt
And then write this in python:
title = subprocess.check_output(['osascript', 'GetNameAndTitleOfActiveWindow.scpt'])
I am trying to run a basic python for .Net application. I have the following installed:
1) Python 33 for 32-bit
2) pythonnet-py3_beta-py3.3-win32.egg installed with easy_install
However, I am getting the following error whenever I work with integers.
For eg: self.notifyIcon.ShowBalloonTip(1000) and timer.Interval = 60000 are throwing the following error
System.EntryPointNotFoundException: Unable to find an entry point named 'PyNumber_Int' in DLL 'python33'
Please find the complete program below:
import win32gui
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Drawing import Icon
from System.Windows.Forms import (Application, ContextMenu,
MenuItem, NotifyIcon, Timer)
class Main(object):
def __init__(self):
self.initNotifyIcon()
self.onTick(None, None)
# timer = Timer()
# timer.Interval = 60000
# timer.Tick += self.onTick
# timer.Start()
def initNotifyIcon(self):
self.notifyIcon = NotifyIcon()
self.notifyIcon.Icon = Icon("app-icon.ico")
self.notifyIcon.Visible = True
self.notifyIcon.ContextMenu = self.initContextMenu()
def onTick(self, sender, event):
self.notifyIcon.BalloonTipTitle = "Hello, I'm IronPython"
self.notifyIcon.BalloonTipText = "Who are you?"
self.notifyIcon.ShowBalloonTip(1000)
def initContextMenu(self):
contextMenu = ContextMenu()
exitMenuItem = MenuItem("Exit")
exitMenuItem.Click += self.onExit
contextMenu.MenuItems.Add(exitMenuItem)
return contextMenu
def onExit(self, sender, event):
self.notifyIcon.Visible = False
Application.Exit()
if __name__ == "__main__":
main = Main()
Application.Run()
In PyQT4 there is a QLabel()
While when I use it in PyQT5 it throws an undefined error.
Can anyone one tell me what is Qlabel in the PyQT4 ?
Or maybe I am wrong?
Here is my code:
import sys
import time
from PyQt5.QtCore import *
from PyQt5.QtGui import *
app = QGuiApplication(sys.argv)
try:
due = QTime.currentTime()
message = "Alert!"
if len(sys.argv) < 2 :
raise ValueError
hours, min = sys.argv[1].split(":")
due = Qtime(int(hours), int(min))
if not due.isValid():
raise ValueError
if len(sys.argv) > 2 :
message = " ".join(sys.argv[2:])
except ValueError :
message = "Alert: alert.pyw"
if QTime.currentTime() < due :
time.sleep(20) #20 Seconds
label = QLabel("<p>Message</p>")
You should read the docs I posted. Add this import statement with the others:
from PyQt5.QtWidgets import *
Because QLabel() is in PyQt5.QtWidgets, please try following :
from PyQt5.QtWidgets import QLabel
The problem appeared to be very simple, but I can not find any solution after a day of googling and looking at stackoverflow.
Originally I am developing a simple plasmoid which will send a specific request to local web-server every 30 minutes, parse output and display in a label on panel. I took an example of plasmoid - BWC-Balance - and modified it. Here is the code:
#!/usr/bin/env python
# coding: utf-8
"""
BWC Balance plasmoid
Site: http://bitbucket.org/svartalf/bwc-balance-plasmoid/
Author: SvartalF (http://svartalf.info)
Original idea: m0nochr0me (http://m0nochr0me.blogspot.com)
"""
import re
from urllib import urlencode
import urllib2
import cookielib
import datetime
import sys
import re
import string
import os
import gobject
import commands
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyKDE4.kio import *
from PyKDE4.kdeui import *
from PyKDE4.kdecore import *
from PyKDE4.plasma import Plasma
from PyKDE4 import plasmascript
from PyKDE4.solid import Solid
from settings import SettingsDialog
parsed_ok = 0
curr_day = ''
class BWCBalancePlasmoid(plasmascript.Applet):
"""Applet main class"""
def __init__(self, parent, args=None):
plasmascript.Applet.__init__(self, parent)
def init(self):
"""Applet settings"""
self.setHasConfigurationInterface(True)
self.setAspectRatioMode(Plasma.Square)
self.theme = Plasma.Svg(self)
# self.theme.setImagePath("widgets/background")
# self.setBackgroundHints(Plasma.Applet.DefaultBackground)
self.layout = QGraphicsLinearLayout(Qt.Horizontal, self.applet)
# Main label with balance value
self.label = Plasma.Label(self.applet)
self.label.setText(u'<b><font color=blue size=3>No data...</font></b>')
self.layout.addItem(self.label)
self.applet.setLayout(self.layout)
self.resize(350, 30)
self.startTimer(2500)
def postInit(self):
"""Start timer and do first data fetching
Fired only if user opened access to KWallet"""
self.setLabelText()
def update(self, value):
"""Update label text"""
self.label.setText(value)
def timerEvent(self, event):
"""Create thread by timer"""
self.setLabelText()
pass
def setLabelText(self):
login = 'mylogin'
request = 'curl --ntlm -sn http://some.local.resource'
out_exp = ""
out_exp = commands.getoutput(request)
table_name_exp = re.findall(r"some_regular_expression",out_exp)
tp = '| html2text | grep -i -A3 ' + login
out_exp = ''
try:
cmd_exp = 'curl --ntlm -sn ' + table_name_exp[0] + ' ' + tp
out_exp = commands.getoutput(cmd_exp)
except:
cmd_exp = ''
date_check = re.findall(r"one_more_regular_expression", out_exp)
times_exp = re.findall(r"[0-9][0-9]:[0-9][0-9]", out_exp )
if len(times_exp) != 0 and len(date_check) != 0:
self.label.setText(u'<b><font color=blue size=3>Start: ' + times_exp[0] + u' --- Finish: ' + str(int(string.split(times_exp[0], ':')[0]) + 9) + ':' + string.split(times_exp[0], ':')[1] + ' </span></b>')
else:
self.label.setText(u'<b><font color=blue size=3>No data...</span></b>')
def CreateApplet(parent):
return BWCBalancePlasmoid(parent)
And what I get is the following error:
# plasmoidviewer bwc-balance
plasmoidviewer(25255)/kdecore (services) KServiceFactory::findServiceByDesktopPath: "" not found
plasmoidviewer(25255)/libplasma Plasma::FrameSvg::resizeFrame: Invalid size QSizeF(0, 0)
plasmoidviewer(25255)/libplasma Plasma::FrameSvg::resizeFrame: Invalid size QSizeF(0, 0)
Traceback (most recent call last):
File "/home/grekhov/.kde/share/apps/plasma/plasmoids/bwc-balance/contents/code/main.py", line 116, in timerEvent
self.setLabelText()
File "/home/grekhov/.kde/share/apps/plasma/plasmoids/bwc-balance/contents/code/main.py", line 146, in setLabelText
out_exp = commands.getoutput(request)
File "/usr/lib/python2.7/commands.py", line 50, in getoutput
return getstatusoutput(cmd)[1]
File "/usr/lib/python2.7/commands.py", line 60, in getstatusoutput
text = pipe.read()
IOError: [Errno 4] Interrupted system call
As I understood after several hours of googling: reading from pipe is interrupted with some signal. But the only signal I have is timer. The only recommendation I have found is "get rid of the signal which interrupts your read". And it appears a bit strange and unrealistic for me: read data periodically without timer.
Am I missing something? Maybe there should be used some other mechanism for accessing web-resource and parsing its output? Or "Interrupted system call" is a normal situation and should be handled somehow?
Thanks in advance for help.
It appears that a signal is being delivered whilst the pipe is still reading.
So try stopping the timer before calling setLabelText(), and then restart it again afterwards.
EDIT
You should also try rewriting your code to use subprocess instead of the deprecated commands module. For example:
pipe = subprocess.Popen(['curl', '--ntlm', '-sn',
'http://some.local.resource'],
stdout=subprocess.PIPE)
output = pipe.communicate()[0]