Python doesnt respond after calling filedialog.askopenfilename() from TK - python-3.x

this is my first question:
Im looking to implement a interactive way to look for a path and then load the data frame with pandas.
Im using Tk, when i run the code it seems like the terminal is running a infinite loop.
`import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
file_path_to_open = filedialog.askopenfilename()`
where is my error ? any advice? im looking for something similar to uigetfile from MATLAB in python.

I don't know whether this answer is still useful for you, but maybe for those people having the same issue and coming here via google:
The issue is not caused by the piece of code itself, because that is working fine. See also Quick and easy file dialog in Python?
I assume it is caused because of some background tk processes which were not closed cleanly.
My solution was to restart the python kernel (using Jupyter Notebook with python 3.6) and reimporting. Also, it might help to close all background python processes, which might be still running, via the task manager.

Related

Python file does not work when executed by double click on Windows

Hi there is an issue I cannot work my head around.
Basically the script works very fine when I execute it via Powershell/cmd python ./myscript.py but not when I double click on it.
I checked with a much simpler script to check if the issue was with my Windows/Python3 configuration.
import os
if __name__ == "__main__":
print('hello')
os.system("PAUSE")
And this one works either by double click or by python ./myscript.py
I guess the issue is with my script but no error is returned and it works just as expected when launched in PS so I do not get it.
Would you guys have some debugging insights?
Similar to this old question: Python Script not working on double click

Linux Ubuntu how to start standard application scribus from python eclipse anaconda

I edited my question, hope it is described better now.
I am working on a software that gives me a nice PDF with lots of matplotlib graphics, depending on the data I get.
So think of a database of pages and then the software decides which pages are chosen and filled with changed images, The text stays the same.
So for instance for data1 I get page1-4 and page7 and page 9. For data2 I get page1-4 and page6. Saved as PDF. I am doing this manually with Quark which needs to be changed. I hope I can figure out the scripting to do so.
But for starters I cant start scribus from the developing enviroment. Eclipse Anaconda on Ubuntu.
import subprocess
subprocess.run('scribus')
works fine in terminal, but gives me an error in Eclipse which I cant figure out.
File "/home/b256/anaconda3/envs/test/lib/python3.7/site.py", line 178
file=sys.stderr)
^
SyntaxError: invalid syntax
This seems to be some Python 2 error in the site.py file
???? Is this some anaconda python path error ??
It's not really clear to me, what you want to achieve, but you're welcome to have a look at a script of mine:
https://github.com/aoloe/scribus-script-repository/blob/master/imposition/imposition.py
This is probably a bit more complex than what you are trying to achieve:
the script gets started from the terminal,
if it notices that it has not been started from inside of Scribus (the exception on import scribus)...
... it starts Scribus with itself as the Script to be run.
the script runs again, this time from inside of Scribus...
... now there is no exception when importing scribus and the body of the script runs.
Of course, it's simpler if you start a script that launches Scribus with other scripts.
For you the most important line is probably:
call(['scribus', '-g', '-py', sys.argv[0]] + arguments + ['--', file])
It's starting Scribus from Python
with as little GUI as possible (-g) and
launches the script sys.argv[0]
with a few arguments and
after the -- tells Scribus what file to open.

How to manipulate tkinter GUI from an imported file

I try to create a small GUI with tkinter. To make my code more readable I want to split the code in 2 files. One for the GUI information and one for the process informations. Or is it a bad idea?
So I create a gui.py where I import my process informations from program.py.
gui.py:
import tkinter as tk
from program import *
root = tk.Tk()
btn_Start = tk.Button(text="Start", command=start_loop)
btn_Stop = tk.Button(text="Stop", command=stop_loop, state=tk.DISABLED)
btn_Start.grid(row=1, column=0)
btn_Stop.grid(row=1, column=1)
root.mainloop()
program.py:
def start_loop():
print('disable Start button and enable Stop button')
# what is the code to disable the start button and enable the stop button?
def stop_loop():
print('disable Stop button and enable Start button')
# what is the code to disable the stop button and enable the start button?
How do I tell the button the disable/enable information in my program.py file? I do not understand how I get the information from the gui to the program and back to the gui?
Thanks for your help
For such a small program, it is overkill.
Looking at the tkinter programs I've written, all of them are between 200−300 lines. This is including headers, comments and blank lines. The number of actual code lines is 100−200.
In my opinion that is small enough to comfortably handle in an editor in one file.
Looking over most of my source code repositories, the longest Python files tend to top out at around 230 lines of actual code.
Keeping all the code in one file has significant advantages for a program when you need to install it. Just copy the file and you're done. No need for modules and a setup.py.

How to open program with key input

I want to open the calculator program when I type "Open calculator " . I researched a lot , but didn't get the answer I wanted.Can anyone please answer my question.
There are 2 ways to do this. One way would be access the cmd using Python which can be done by using os module. When you try to open the calculator from your command prompt, you probably type calc. Instead of manually doing this, you can have your Python code do it for you, this is how:
import os
os.system('calc')
The second way is very similar to the first one, except that this method opens another command prompt window so the window in which you're running the python code is not disturbed.
import subprocess
subprocess.Popen("calc",stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,shell=True)

How to exit one python script from another python script but keep the first python script running?

I've a GUI and I'm running another python script from GUI script. Now if user enters incorrect values I want to show error message and stop right there. Basically terminate second script at that point itself and not do anything further. But I'd like the first script (GUI) to stay open. How can I do this? Return function may not be very useful here because return function just stops the current function. I'm calling one function from another several times. Is there an easy way of doing this?
For tkinter you can use messagebox.
from tkinter import messagebox
messagebox.showerror("Error", "Message")
messagebox.showwarning("Warning", "Message")
messagebox.showinfo("Info", "Message")

Resources