How to exit one python script from another python script but keep the first python script running? - python-3.x

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")

Related

Python Tkinter: Run python script when click button_widget of tkinter and keep value from return variables

I am new with GUI programming in Python 3.x with Tkinter.
I have prepared a GUI where user needs to select options (using OptionMenu widget) and after selection press button to run the final program.
I have saved the user selected data into variables in that GUI program.
But don't know what should I do next...
What I want:
That GUI should be hidden or End after pressing the button.
Run another python script and use those saved variables from that GUI in my script.
How it can be done in python.
You can save your data in another file in order to use it in other script with pickle module.
To save you can do a list with all the variables you want:
import pickle
with open('doc_name.txt','wb') as a:
pickle.dump(saved_variable_list,a)
And in another python script you can use that list of variables:
import pickle
with open('doc_name.txt','rb') as a:
saved_variable_list = pickle.load(a)
Finally to close your GUI you can use the 'destroy' command:
root.destroy()

How to run python scipt in the background(after user input) even if the terminal is closed

I have a python script that takes some inputs from the user & then executes the code based on the input. The code takes some time to complete; during this code runtime the user can close the terminal(the code is run from a Linux machine)
As soon as the user closes the terminal the script stops as well. I know there are options like nohup but it wouldn't accept any input(where input is required in my script).
How can I fix this?
Requirement is -
Run the script, enter the inputs
Let the code run in the background even if the terminal is closed
Also is there a way to write whatever is being printed in the terminal(during the script runtime) to some file
Linux's screen tmux served my purpose.
There is a work around possible,
you can split your programm into two parts.
And start your background task with:
import os
usr_inp=input("test input: ")
os.popen(f"python3 background_task.py {usr_inp} &")
This should start the other programm in the background, there you can use the input over the sys.argv[1] variable.
(Usually it's not recommended using os.popen)

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)

Python doesnt respond after calling filedialog.askopenfilename() from TK

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.

Press enter stops computing python

I'm doing some engineering analysis with the help of a FEA program and Python. When the analysis ends I need to press a key to continue. But this is not a normal press any key to continue. Every code executed with the scripts stops. Like a handput debug break. Nothing runs until I press something or switch windows.
I cant use send keys and subprocesses because running code completely stops. Only solution I could come up with is to use another script in another command window with simple send keys command. This extra script is useless if computer is used or another window is active.
I'm a beginner level programmer and maybe I'm missing something simple. I guess the problem is caused by the FEA programs code but I'm not sure. So is there any way to prevent my code from stopping? Thank you for your time.
It seems that the FEA program does the windowing and you cannot do much about it. I actually automate scripting in DIANA FEA. For this program I would try something like pywinauto.
https://github.com/pywinauto/pywinauto
And call your python script from another python script.
from pywinauto import Desktop, Application
import time
app = Application().start("FEA_program.exe my_python_script.py")
while True:
time.sleep(5)
# send key presses to the app every arbitrary seconds

Resources