I am trying to develop a python tkinter based app that would read the data from my arduino sensor platform.
I am getting inconsistent errors that i can't understand why this happens.
First of all, this is the code that is to blame:
def readSerial():
print("1")
global after_id
print("2")
ser_bytes = ser.readline()
print("3")
ser_bytes = ser_bytes.decode("utf-8")
print("4")
text.insert("end", ser_bytes)
print("5")
after_id = root.after(50, readSerial)
Sometimes this works. The data appear on my tkinter frame, as the characters 1,2,3,4,5 keep scrolling down in the terminal. This is the rarest case.
Other times the terminal printing keeps on going down, but nothing appears on my tkinter text frame. When i press the read button again, the numbers stop printing in the terminal, but the text from the serial appear on my tkinter frame. How is this possible i cannot understand.
Some times the terminal printing keeps on going down, but nothing appears on my tkinter text frame. If i press the 'start reading' button again, the program will get stuck, the button will remain gray, the print in the terminal will stop.
Other times i get freeze for some reason. Make the program will freeze, or the stop button will make it freeze.
Not only i cannot understand why the behaviors except (1) - the normal case occur, i don't understand why there is no consistency in what behavior will be triggered. When behavior 2, or 3 or 4 will happen and why? What triggers each specific behavior?
Can someone understand what is going on?
I would ask myself about your setup (which I don't know what the circuit is):
have you tested your code with test data that works independently of a link to your Arduino.
have you checked the validity of the data coming from the Arduino.
what measures are you taking to avoid noise being introduce with your data input.
Related
I am programming a GUI in plain C using X11 library, on Cygwin. When trying to read cursor movements over an open window using XMaskEvent() it works like a charm. It returns an XEvent structure whose coordinates point to the current mouse location and the event_type is a small positive integer number that corresponds to what happened (mouse move, button press, button release etc). So far so good. The problem is that this command blocks (it is supposed to) after each mouse move or button event so my program cannot do anything continuous.
So I tried to replace XMaskEvent() with XCheckMaskEvent() which should read an event, remove it from the queue and continue, so that my program can continue doing something. However, XCheckMaskEvent returns an event with zero coordinates, and constant crazy event_type of minus million-something. Whatever i do with the mouse or keyboard, I am always getting an empty event of a crazy type.
I am using the same event mask for both commands.
What am I doing wrong?
the problem = I dont know how to get a program to detect a input from the keyboard without using IDLE
A solution to my problem would look like; some code so that when a key is pressed it sets/makes a variable.
(I am doing this on a raspberry pi)
eg. code code code 'user presses space bar' pi camera takes picture and ends the preview. I dont want it so that the input is in the python shell, just when the space bar is pressed. just to note I am a beginner coder who knows next to nothing so if you can help or point me in the right direction I would be grateful.
edit:
here is the code I have so far
from picamera import PiCamera
from time import sleep
import webbrowser
#importing all the needed things
webbrowser.open('/home/pi/RyansScienceFair/test.html')
#opens a webbrowser that I coded
camera = PiCamera
camera.start_preview(alpha = 220)
sleep(10)
camera.stop_preview()
#runs the camera for 10 seconds with some transparency
what I want is when the user is looking at the web page, which will be displayed under the camera display, they can press spacebar and then the program takes a photo and then ends the camera preview. I can do the camera capture and work out the end time for the camera but its the trigger that I cant figure out.
Without the cv2.waitkey() method the cv2.imshow() displays black window. Why does the rendering does not happen properly without the wait?
cap = cv2.VideoCapture(video_path)
while cap.isOpened():
ret,frame = cap.read()
cv2.imshow('window-name',frame)
# without the following cv2.waitkey(1) statement the cv2.imshow() displays a blank window
if cv2.waitKey(1) & 0xFF == ord('q'): # wait for 1 millisecond
break
continue
From the documentation of cv2.imshow(), the NOTE section mentions that the window is displayed for the amount of time indicated by the argument in cv2.waitKey(). An argument of 0 indicates wait forever, so the image will be displayed forever unless you handle the keypress.
Controlling the duration for which the window needs to displayed is a useful aspect while debugging, displaying intermediate images, etc.
From the documentation of cv2.waitKey(), the NOTE section mentions that 'This function is the only method in HighGUI that can fetch and handle events, so it needs to be called periodically for normal event processing unless HighGUI is used within an environment that takes care of event processing.'
You can notice that without the cv2.waitKey(), if you hover over the window that is displayed, the 'busy' cursor with the rolling wheel is displayed, indicating that the window is busy.
while True:
x=pyautogui.position()
print(x+pyautogui.pixel(x[0],x[1]))
and program is picking up color that is actually upper left of my cursor...
I am using python 3.6, windows 10, on 125% magnification. is this one of the reason?
Usually picking color with the mouse gives NOT the color of the mouse pointer on the screen but the color of the screen 'below' the cursor.
On my box (Linux Mint) the code:
import pyautogui
while True:
posXY = pyautogui.position()
print(posXY, pyautogui.pixel(posXY[0], posXY[1]) )
if posXY[0] == 0:
break
delivers what it should - the color 'below' the mouse pointer.
Notice the 'break' in the endless loop. This allows you to stop the loop by moving the mouse to the left edge of the screen :)
I have the same setup as yours (but Python 3.7) and faced the very same issue.
Why it occurred? Every now and then I connect and disconnect the external monitor from my laptop. This causes certain scaling issues every time. So, I tried restarting my computer with the external monitor connected and did not disconnect it until I ran this script. It worked like a charm. BTW, I also tried just restarting the Windows Explorer, but this did not resolve the issue.
tldr; Restart your PC and do not modify the display configuration since the restart
It is possible to read the following in the apple Documentation:
UIKeyboardFrameBeginUserInfoKey
The key for an NSValue object containing a CGRect that identifies the start frame of the keyboard ……
UIKeyboardFrameEndUserInfoKey
The key for an NSValue object containing a CGRect that identifies the end frame of the keyboard ……
Would that mean that the keyboard has a "start frame" and an "end frame"?
I suppose YES.
But when the keyboard appears I cannot see any frame changing. It just stays the same frome start to end.
So my question is:
What are those "start frame" and "end frame" referring to?
I must be missing something.
Thanks for your help.
The keyboard does indeed have a start and end frame, and the properties do exactly what you suppose they do. They keyboard does not always animate however; sometimes it just appears or changes size. For example, in the case that you are typing on the Japanese keyboard, when the keyboardWillShow fires after the first character is hit. There's no animation, but an additional bar appears above the keyboard, thus changing the size. The properties you listed above tell you how much the keyboard changed size by.
I'm not sure what exactly you're looking at when you say no frames are changing. I suppose it's possible that when you move from one editable text field to another, you get a keyboardWillShow notification, even though nothing on the screen changes.