In order to print stuff on campus, I need to login to the webpage and submit the printing job manually on a browser, but I wanted to submit a job locally. So I wrote a small python script that uses Selenium to automate print job submission. I've verified that it works, but there's one thing that bugs me. Even after I select a file to print, the file dialog stays there until the actual code runs to the end.
The structure of the code is like the following.
1. Enter information on terminal (username, pw, which printer to use, how many copies, etc.).
2. Call tkinter.Tk().withdraw() to select a file (after selecting a file, "Submitting a print job..." is printed, as shown on the screenshot)
3. Do the actual Selenium job using information I collected above to submit a job
How can I make that file dialog disappear as soon as I select the file? Is it an ascyncio problem?
I noticed that many other people were experiencing a similar issue. While many had solved the issue by adding Tk().update() before or after askopenfilename() line, my problem was only gone when Tk().update() line was added both before and after the askopenfilename() line. FYI, I'm running the script on macOS with python 3.7.
Related
I am working on automating program that requires choosing file path.
After I click the browse button using code:
dlg.child_window(auto_id='btnBrwsBinFile').click()
browse window opens and freeze execution of code and I cannot control the popup window.
I have tried also different approach. I have edited text filed using code:
dlg['Edit'].set_text(path)
but then program do not see the path, it treats the field like it was never edited, like it was empty
I would like to ask if someone solved this issue before.
try:
dlg.child_window(title='btnBrwsBinFile').click_input()
pywinauto documentation:https://pywinauto.readthedocs.io/en/latest/code/pywinauto.findwindows.html
You can use UISpy.exe to get the title, class name
This must be the most weird and confusing problem I've ever seen. So I've been trying to open an html file with python using the:
import webbrowser
webbrowser.open_new_tab()
method, at first it worked as intended but then problems started to appear. When I ran the program the file would open, but after 1 second it would close. This would always happen when I ran the program with VS code but when I ran the python program in IDLE (v3.10.0) the program ran just fine. I'm out of my depth here and I have no idea what could be causing this problem.
def visit_website():
print('Would you like to visit our website ?')
def_a=input()
if def_a=='yes' or def_a=='Yes' or def_a=='ok' or def_a=='Ok' or def_a=='OK':
webbrowser.open_new_tab('demofile.html')
I have written code in a New file window in IDLE.
When I run the code there is no output.
Instead a dialog box appears showing a window accessing Python Folder 37-32.
When i closed the dialog box and the file I tried to create a new simple code below but I when I ran the code I got the same Dialog box.
What is wrong?
sum = 2+3 print(sum)
I have attached a screenshot showing the code and the dialog box that appears when the Module is run
Before you can execute your code, your first need to save the file. Thats the dialog box that popped up.
You should have seen a popup box like below. Did you? Is is unclear?
Save Before Run or Check
? Source Must Be Saved
OK to Save?
[OK] [Cancel]
One reason to require saving is that exception tracebacks refer to the file and line of lines that lead to the exception.
If you had saved, sum = 2+3 print(sum) would be a SyntaxError.
You can run single statements in Shell without saving.
The RESTART lines says that the shell re-initialized the environment for executing your code.
You should normally not save your code buried in the installation Scripts directory. Better to make a directory in your user directory, for instance, C:/Users/yourname/py/.
Yes, one should usually open a new question for unrelated questions. But without access to your machine, it is hard to know what happened with 'new'. It may be that IDLE could create a file under .../appdate/.../Scripts/, but your code cannot. If the open call did not raise and exception, it was likely created somewhere. Until you are more experienced, better to use absolute paths, such as C:/Users/yourname/py/new.txt.
Okay so I am new and still learning all the time and I am trying to wire up a GUI for my code. My code works perfectly in Python IDLE shell and as of so far, works perfectly in my GUI I have created. I have yet to learn
how to multithread processes, I would like multithread all of my functions and redirect output and input from IDLE shell to the three text boxes in my GUI. As of so far, I am pressing my buttons and they work as intended,
all input and output from after pressing a button is in the IDLE shell and my next step is to feed input and output to my text boxes in my GUI, so that my migration from IDLE shell to my GUI is complete and so that my GUI
can 'standalone' independent from the IDLE shell of course. The three text boxes include, 1. for text input, 2.for listing results. and 3. for actively displaying my update features. The program I am making is a file search
method by way of indexing, it works fine in the shell and i am migrating to my GUI. In the shell, when I update say Text files,
the code runs in and out of directories making a list of all text files on the system and saving them in a file, it also does this for video, audio, executable and image files. When the update is executed, files with the
appropriate file extensions for the particular update are displayed in real time in the IDLE shell, streaming down the IDLE shell very fast, this is hopefully the job for text box three, these results will be displayed in
text box three as they are found, for feedback purposes. Text box 1 is user input, the file name to search for, when in IDLE shell, the name entered is searched for in the lists made by my update feature, instead of
scanning the system every time. The result/(s) is then displayed in the shell, this works perfectly and can be opened/played/executed if the user wishes by appending the found path+filename to the appropriate module. This
brings me to text box 2, where in the shell, file results from a search are displayed where they will hopefully now be migrated to text box 2, which is where search results will be displayed from now on.
I am asking for examples how i my case I would achieve this migration from shell to GUI. I will post my code as is and perhaps someone with much more experience can provide some examples on how to re-wire this.
Thank you in advance for any help, Benjamin
When a user runs code with IDLE, output sent to stdout, either with sys.stdout.write or print() with file left as sys.stdout, appears in the tk(inter) Text widget of IDLE's Shell because IDLE replaces the default sys.stdout object with with an object that sends sends strings to the Text widget. Since this involves usually inter-process communication, the code will probably not of must use to you.
What might help is the following: Find in Files (grep) puts found lines in an OutputWindow with a write method that accepts a string and returns the number of characters written. Here is the 3.x version.
def write(self, s, tags=(), mark="insert"):
if isinstance(s, (bytes, bytes)): # convert to string if necessary
s = s.decode(IOBinding.encoding, "replace")
self.text.insert(mark, s, tags) # insert into widget
self.text.see(mark) # make insertion visible
self.text.update() # update widget
return len(s) # return # chars written
I presume you can rewrite this for (py)qt's text widget. The alternative is to use the widget methods directly.
I had a long script running on iPython notebook in Firefox for a long time. I came back and it seemed to have hung up, so I saved it and closed it.
When I re-open the script, I get a dialog box pop up with the following error:
Warning: unresponsive script
A script on this page may be busy, or it stack overflow may have
stopped responding. You can stop the script now, open the script in
the debugger, or let the script continue.
The options are 'continue', 'stop script' and 'debug script'. Clicking on any of these leads to the same result, the dialog box disappears and the iPython notebook is unresponsive (for example, ctrl+enter creates a line-break in the current cell rather than executing it).
Worst of all however, the cells at the bottom of the script seem to have been cut off. They contained some valuable code which seems to have gone, this is quite a bad outcome!
I've tried rolling back but the last roll-back point also shows the bottom cut off. Any support here much appreciated!
I have now worked this out and leave it here in the hope it will help others.
The cause of the problem seemed to be an excessively long output from one of the cells - everything below this line had been cut off in the iPython browser, but I discovered it still exists in the .ipynb file and all I had to do was remove some of the output lines, and when I re-opened the file it ran without problems AND my code that had been cut off was available once again.
The notebooks store everything in JSON format. In my case, I needed to remove output from one of the cells, which I did like this:
Browse to your iPython Notebooks directory (NOT where ipython.exe resides) - for me they were in C:\Users\myname\Documents\IPython Notebooks
Right-click on the offending notebook.ipynb file, and edit in a text editor - my choice is Notepad++
Scroll down to the cell which has generated lots of output lines. Each of these lines is inside the cell's outputs property, with "output_type": "stream"
Remove an arbitrary number of these output entries, but be sure to remove anything outside the output property itself, and be sure to remove from the back of a tailing comma to the front of the following comma so that the resulting JSON is well-formed
A typical line of output looks like this, deleting several hundred of them made my script run again in the browser:
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Added 150000 records so far"
]
},
Maybe it's Firefox's fault.
Letting the script run longer
If you find that pressing the Continue button brings up the same dialog again, letting the script run longer won't help you; it will just make Firefox hang for longer. However, if you can use Firefox normally after pressing Continue, then the script may just needs extra time to complete.
To tell Firefox to let the script run longer:
In the Location bar, type about:config and press Enter. The about:config "This might void your warranty!" warning page may appear. Click I'll be careful, I promise! to continue to the about:config page.
In the about:config page, search for the preference dom.max_script_run_time, and double-click on it.
In the Enter integer value prompt, type 20.
Press OK.
With scripts now allowed to run for longer times, you may no longer receive the prompt.
This is from https://support.mozilla.org/en-US/kb/warning-unresponsive-script
Hope it could help.