I'm working on a script using the PyAutoGUI module. Sometimes the script gets stuck in a while loop because it's looking for pictures/images that are not shown due to connection problems etc. If this happens I want the program to start again from zero, so I want to simulate the play/run-button in Pycharm with a command line. Is this possible?
What it sounds like you want to do is restart your program if it doesnt respond. A similiar question appears to have been posted here: Python help - Need the ability to restart the script when it hangs or automatically set a timer so I would reccommend having a look at that. Simulating the run button in Pycharm might seem like a good idea at first but it is very specific and a bad practice to simulate user actions like that unless there is absolutely no viable alternative.
Thanks to Patel I managed to solve my issue. You can use ctrl+f5 to restart a script so now when I'm stuck in a while loop I'm using this code:
#Click toolbar Pycharm
pyautogui.moveTo(1672,15,1)
pyautogui.click()
#Rerun the script
pyautogui.hotkey("ctrl","f5")
Related
I have been recently been trying out this app called compiler(on android) and whenever I try run code it is asking for something called a runtime input? I am new to coding so I really don't know what it means. Another issue is that when I try to run inputs e.g input("hi: ") I am unable, to type anything in the terminal, wondering if there is any fix? In advance thanks to anyone who responds.For some reasons the staic terminal does not apply to other ides such as pycharm or pydroidenter image description here
This is a common issue in the most of the online ide. You have to provide your input in that alert box and hit run.
The reason it happens is because your code is not running on your device instead running on a remote server which only allocates limited time(3-5 seconds) for your code to execute, so to make full use of that time, you are asked to give all the input beforehand so that program doesn't have to wait for user input and waste that precious time.
Also, you can't expect prompt messages to work here as they are working in an offline ide.
I have this really strange problem, basically I want to start xpdf (or Libreoffice) from my Python script, that is started by a systemd-service. When I start the script from terminal everything is working fine, but when I plug in my USB device that start the Service, I'll get this Error in my syslog:
sh[2321]: Error: Can't open Display
This error has something to do with X11, that's what my Google searches tell me.
So, my question is: How can I properly run a program like xpdf or libreoffice from Python?
import subprocess
subprocess.call("/usr/bin/xpdf")
This is it, basically. I know that it has something to do with the graphical enviroment, but I don't know how I can solve it.
The X display system has very good security to stop random local processes from just displaying stuff to the local screen (It was more a problem in the old days of expensive Sun and SGI systems where computer labs would often let users telnet to other boxes. Much fun could be had!).
If the user running the xpdf is the same user as the one who's logged into the X session, then you simply need to tell xpdf where to connect it's UI to. This is usually done by exporting DISPLAY=:0 to the environment, which means "connect to the first local screen". Most X programs also support -display :0 argument.
So do:
/usr/bin/xpdf -display :0
or:
DISPLAY=:0 /usr/bin/xpdf
It's very unlikely that you have more than one X session so :0 will work 99% of the time.
Since the issue is that xpdf isn't finding a display to connect to, we have two basic options: find and authenticate with an existing display, or make a new one. The latter is usually easier, something like:
xinit /usr/bin/xpdf -fullscreen $PDFFILE -- :2
This would start a new X display :2 running only xpdf, not even a window manager.
It finally worked, after trying and going crazy for around 2 weeks.
What worked was
os.system("DISPLAY=:0 /usr/bin/xpdf)
I know that subprocess.call is the better way to call the program, but it doesn't seem to work right now.
I'll try the way that Yann suggested later on, but for now I'm just overwhelmed with joy that it just works.
Thank you all for your help, I really appreciate it!
I'm writing a simple program that gives you a message to take a break from sitting at your Computer every x minutes. However, I also need to be able to close the program without having to wait for a window to pop up after x minutes.
My ideas are that I either create a systray icon (I'm programming it for a friend who uses Windows) and add the possibility to exit it that way, or that I just add another window, which has an exit button and stays open all the time (less elegant).
From what I have read, it seems that the systray idea requires something more complex than easygui.
So, is there a way to implement any of those ideas with easygui and if not: what do I need to look at to get it working?
Thanks for your time and effort.
In my program I am trying to take the chat from a website and printing it on my console. While that's going on I'm using raw_input to get chat from whoever is using it. My problem is that raw_input pauses the rest of the script until i say something or press enter. Is there a simple way to fix this?
You have to multithread. One thread for user input and another for the background tasks.
The documentation is a bit complex (I'm pretty confused by it), but it's a start: http://docs.python.org/library/threading.html
You may also want to look into the curses module: http://docs.python.org/library/curses.html
I wrote a little PyGTK app: Workcycler
Now I have the problem that I need to hit the quit button twice for it to quit and I don't know why.
I´ve tested it extensively and it allways calls all quit functions but the program simply doesn't close after the first time.
It's a pretty short script so could someone please have a look over it?
I think the problem may come from using python's timers or a part from the pygame lib (mixer).
These are the important files I think: workcycle.py and tray.py
You are running the workcycle.ui.tray.WorkcycleTray.start method twice (so gtk.main run twice )
once in the workcycler and another in workcycle.py line 20, comment that line and everything works fine.