I have two monitors (one is 1920x1080, the other is 1024x768) and I've just started a new game and ran into an issue with pg.display.Info:
import pygame as pg
pg.init()
x,y = pg.display.Info()
It gives me the following error:
Traceback (most recent call last):
File "C:\Users\marty\Desktop\New Text Document (2).py", line 3, in
x,y = pg.display.Info()
TypeError: 'VidInfo' object is not iterable
Even after isolating the code to those three lines, the same error gets raised. I couldn't find any info on 2 monitors in Python.
PS: pygame is updated, according to pip. My Python version is 3.6.5, and I'm running windows 10.
You can't unpack the VidInfo object which pg.display.Info() returns like that. Assign it to a single variable and then use the attributes that you need. For example:
info = pg.display.Info()
print(info)
print(info.current_w, info.current_h)
Related
First of all I want to sorry for my bad English.
I'm trying to use the ev3dev2 library and Visual Studio Code to add python code to my ev3 robot. My problem is that when I try to use the function 'animate_stop' (or 'animate_flash', 'reset', and some others) from the class 'Leds' I get an error saying that the called function isn't an attribute of the object 'Leds' but when I opened the 'led.py' file (which contains the 'Leds' class) I found all the functions that I tried to call.
I have installed ev3dev2 from github and the official SD card image file for the ev3 from its site.
The code:
#!/usr/bin/env pybricks-micropython
from ev3dev2.led import Leds
Leds().animate_stop
The error:
Traceback (most recent call last):
File "/home/robot/ttt/main.py", line 5, in <module>
AttributeError: 'Leds' object has no attribute 'animate_stop'
Try this:
#!/usr/bin/env pybricks-micropython
from ev3dev2.led import Leds
leds = Leds()
leds.all_off()
I have a pyROOT script where I use TChain::AddFriend to combine two TTrees:
from ROOT import TFile, gDirectory
myfile = TFile("e.root")
mychain = gDirectory.Get("elec_gen")
entries = mychain.GetEntriesFast()
friendFile = TFile("mu.root")
friendChain = gDirectory.Get("muon_gen")
mychain.AddFriend("muon_gen")
elec_gen_evtNum = mychain.evtNum
muon_gen_evtNum = mychain.muon_gen.evtNum
When I run this I get:
$ python friendTest.py
Traceback (most recent call last):
File "friendTest.py", line 12, in <module>
muon_gen_evtNum = mychain.muon_gen.evtNum
AttributeError: 'TTree' object has no attribute 'muon_gen'
With the last line commented-out it runs fine. So it appears I am not accessing the leaves of the friend tree (muon_gen) correctly. How do I access them?
I also tried combining the TTrees using:
mychain.AddFriend("muon_gen","mu.root")
but this produces the same error.
I had a similar question, and found an answer (well, workaround) on the ROOT forum:
https://root-forum.cern.ch/t/accessing-a-friend-ttree-using-pyroot/25513
although no solution was presented using TFriend.
Instead, RobS found a workaround for his own question of just loading the two TFile and TTree separately and running LoadTree() and GetEvent() on each TChain
I'm trying to use locateCenterOnScreen() function of PyAutoGUI, however it raises :
Traceback (most recent call last):
File "C:\Users\windows\Desktop\asd.py", line 3, in <module>
buttonx, buttony = pyautogui.locateCenterOnScreen('who.jpg')
TypeError: 'NoneType' object is not iterable
My code is:
import pyautogui
buttonx, buttony = pyautogui.locateCenterOnScreen('who.jpg')
pyautogui.doubleClick(buttonx,buttony)
How can I fix this problem?
From the documentation of Pyautogui here, the method locateCenterOnScreen returns None when it can't find the image on your screen.
Note that you are looking for 2 results from this method, but None is just one result (since the method normally returns two, this seems like bad design to me -- it should raise an exception instead, or at least return a tuple with two None objects).
Look at the following example, which is basically what is happening to you:
>>> foo,bar = None
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable
The simplest and most Pythonic way of addressing this in my opinion would be simply to try and catch it:
try:
buttonx,buttony = pyautogui.locateCenterOnScreen('who.jpg')
except TypeError:
""" Do something to handle the fact that the image was not found"""
EDIT:
To answer your question raised in the comments, there seems to be a misunderstanding with how this library works, or what it finds on the screen. You give the library a representation of what it needs to find via some image. It works far better when that image is lossless, because then it's an exact, pixel-by-pixel representation. The library then searches your computer screen for the actual thing represented by the image provided. It does not, as you raised concerns, find jpegs or pngs. It finds the actual rendered object. So, if you take a screen shot of the icon for your web browser on your desktop, it will find the actual icon from that screenshot and click on it, but only if it's visible. If it's behind other windows or something, it won't find it. It doesn't search the screen for the icon file, but the rendering of the icon itself. So, for instance, if you provided the actual .ico file to the library, it would not be able to find that icon if it was covered by another window, even though that icon is technically on your desktop, because it's not currently rendered.
To SIMPLIFY things - a locateCenterOnScreen() method should return the center (x,y) coordinates of an image as you called 'who.jpg' ONLY IF IT EXISTS ON THE SCREEN. Otherwise, if the 'who.jpg' image is not found on the screen - THE METHOD SHOULD RETURN 'NONE'.
You can not assign a 'NONE' value to (x,y) coordinates because you get a single value ('NONE') and two variables that are waiting to receive some value. This why you get a "TypeError".
Try to use an Exception, which is an event that may occur during the execution of a program -> for example a TypeError. Try to predict even other events that may occur during execution and you will be good to go with your task!
FOR YOUR CONVENIENCE TRY THE FOLLOWING CODE :
try:
buttonx, buttony = pyautogui.locateCenterOnScreen('who.jpg')
pyautogui.click(buttonx, buttony)
except TypeError:
print("A TypeError has been occured!")
if instead of x,y you just use one variable x,y would be on [0], and [1]
so it would be something like
location = pyautogui.locateCenterOnScreen(path)
then if not found it would return
location = None
else would be
location[0] = x and location[1] = y
If size of your image is 1920x1080, the image calls take about 1 or 2 seconds.
Therefore, try the code below.
import pyautogui
import time
time.sleep(2)
buttonx, buttony = pyautogui.locateCenterOnScreen('who.jpg')
pyautogui.doubleClick(buttonx,buttony)
You will be able to solve your difficulties.
Background
I am using pyqtgraph to make an interactive program that plots and analyzes some data.
Versions of relevant things:
PySide version 1.2.1
from PySide import QtGui, QtCore
pyqtgraph version 0.9.10 (latest at the moment):
from <my own package>.external import pyqtgraph
On Ubuntu 14.04.3 LTS
Python 2.7.6
Code Structure
self.w.dotPlot is a pyqtgraph.PlotWidget object
I draw a box by doing this:
self.timeBox = self.w.dotPlot.plot(x=self.baseTimeBoxX,y=self.baseTimeBoxY,...)
where:
from numpy import r_
self.baseTimeBoxX = r_[0.0,0.0,100.0,100.0,0.0]
self.baseTimeBoxY = r_[-1.0,1.0,1.0,-1.0,-1.0]
self.w.timeBox is thus an instance of pyqtgraph.graphicsItems.PlotDataItem.PlotDataItem
When the user clicks on the plot, I want to move the box in the X direction only. To do this, I figure out the clickedXCoord, add this to self.baseTimeBoxX, and now want to update self.w.timeBox to use these x coordinates. To do so, I call
self.w.timeBox.setData(
x=(clickedXCoord+self.baseTimeBoxX),
y=self.baseTimeBoxY
)
The Problem
Here is the traceback that I get:
Traceback (most recent call last):
File "doesntMatter.py", line <whatever>, in _moveTimeBox
self.w.timeBox.setData(x=(clickedXCoord+self.baseTimeBoxX),y=self.baseTimeBoxY)
TypeError: 'PySide.QtCore.QPointF.__add__' called with wrong argument types:
PySide.QtCore.QPointF.__add__(numpy.ndarray)
Supported signatures:
PySide.QtCore.QPointF.__add__(PySide.QtCore.QPointF)
Things I have tried:
Instead of changing the data in the existing PlotDataItem using self.w.timeBox.setData, I tried just creating a whole new one:
self.w.timeBox = self.w.dotPlot.plot(
x=(clickedXCoord+self.baseTimeBoxX),
y=self.baseTimeBoxY
)
I got basically the same error
Im working with Agilent IVI drivers in Python 2.7.9 and can't seem to get 'proven' code to work on a particular Windows 7 machine. It executes successfully on other machines.
While this issue seems rather limited to one instrument, it appears to be a broader Python issue, so I turn to Stack Overflow for help. Any help or insight would be great.
The following code
# Import the TLB
from comtypes.client import GetModule, CreateObject
GetModule('AgInfiniium.dll')
# Pull in the coefficients and classes, we'll need those
from comtypes.gen.AgilentInfiniiumLib import *
# Instantiate the driver
OScope = CreateObject('AgilentInfiniium.AgilentInfiniium')
# Make constants of our settings
Address = "TCPIP0::10.254.0.222::INSTR"
resetOScope = False
# Open a connection to the scope
OScope.Initialize(Address,False,resetOScope,'')
# Make a measurement
print OScope.Measurements.Item("Channel1").ReadWaveformMeasurement(
MeasFunction=AgilentInfiniiumMeasurementAmplitude, MaxTime=10)
yields the following error:
Traceback (most recent call last):
File "P:\Aperture\Validation\WMI_BGA_Board\TestMatrixCode\scopeTest2.py", line 29, in <module>
print OScope.Measurements.Item("Channel1").ReadWaveformMeasurement(MeasFunction=AgilentInfiniiumMeasurementAmplitude ,MaxTime=10)
File "C:\Python27\lib\site-packages\comtypes-1.1.0-py2.7.egg\comtypes\__init__.py", line 656, in call_with_inout
rescode = list(rescode)
TypeError: 'c_double' object is not iterable
In my limited debugging attempts, I have seen that this call_with_inout
function tries to convert my Python arguments into arguments for the following C++ function:
public void ReadWaveformMeasurement(
AgilentInfiniiumMeasurementEnum MeasFunction,
AgilentInfiniiumTimeOutEnum MaxTime,
ref double pMeasurement)
It's creating some kind of variable for the pMeasurement pointer that ends up being type c_double, and then complains that it's not iterable.
At this point, this seems like it's local to this machine. I've gone to the extent of uninstalling Python, reinstalling the Agilent driver, and trying two versions of comtypes (1.1.0 and 1.1.1). Yet the problem persists. Any ideas? Thanks.