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

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

Related

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 can i open a file using a python script i coded?

I would like to open a file such as an image file using a python script, and by doing so, pass the file name and location into the script when i choose to open the image in my script.
clicks "open image in another program" in windows 10
import tkinter as tk
filename = fileopen.name()
filelocation = fileopen.path()
window = tk.Tk()
imagepic = tk.PhotoImage(file=filelocation)
picture = tk.Label(window, image=imagepic)
window.mainloop()
I'm pretty sure theres a module that allows for this but i just cannot find it.
I'm not entirely sure I understood your question correctly, but for dealing with the operating system you would use the module os:
import os
os.startfile(filelocation) # Open file in win10 default program
What I meant originally was how to go about registering a context menu option in windows explorer allowing the user to right click a file and select "Open with " - passing the file into the script as an argument.
In order to achieve this, you can follow this guide, supplying this command:
python -m <absolute/path/to/yourscript> %1
The %1 will be the file as an argument.

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

Linking a command line application to a tkinter GUI

I am currently trying to write a User Interface for a program, that is only used via command line. It has several mandatory and optional parameters but you really only need one line to execute it. Now I am wondering how I can "link" my python script to the command line and make it execute the program when I click a "Run-Button".
I am just starting to get into TKinter and Python programming, so I need very basic instructions.
I am on a Linux-System (Ubuntu 16.10) using Python 3.5 and Tkinter.
Thank you.
For command line use, check this module: subprocess
In your GUI code
create a start Button:
Button(master, text='RUN', bg='green',fg='white', command=Run-Button).grid(row=0, pady=4)
and a function definition attached to it:
def Run-Button():
subprocess.call(["ls", "-l"]) #use your command to run

Vim Python: better way to call python code?

function! My_dir(fname)
python3 << EOF
import vim
blah = vim.eval('a:fname')
if str(blah) == 'None':
cb = vim.current.buffer
cb[0] = ' .vimrc'
cb.append(' .vim/plugins/')
cb.append(' newbie.vim')
EOF
endfunction
Is there a cleaner way to call python code? Any way to stick common imports in a file, vs doing import vim everytime - within the python block?
Create a X.py file and import every thing that you use commonly. For example as I usually need os and sys libraries in my programs, my CommonModules.py is this:
import os
import sys
Then put this file in a directory and create an Environmental Variable and name it PYTHONSTARTUP. and assign the path of that file as its value.
Now every time you open python, the CommonModules.py called automatically.
How to set environment variables :
Windows Vista and Windows 7 users
From the Desktop, right-click the Computer icon and select Properties. ...
Click the Advanced System Settings link in the left column.
In the System Properties window, click on the Advanced tab, then click the Environment Variables
button near the bottom of that tab.
And for linux Users:
https://unix.stackexchange.com/questions/117467/how-to-permanently-set-environmental-variables

Resources