GUI Interface For Command Line Input/Output - python

I was hoping somebody could point me in the right direction for a project I would like to do. My intention is simple, to have a GUI that allows a user to input a string, that string fills into a pre-determined line of command line text, runs through the command line and returns what is printed on the command line screen. I've been leaning toward using Python for this but I am still not sure about the syntax that will accomplish even the first part, where a user will input a string and that string will run through the line of command line text. Any sort of input would be greatly appreciated!

This is a simple GUI using tkinter for python
try:
import tkinter as tk # python v3
except:
import Tkinter as tk # python v2
# This function is called when the submit button is clicked
def submit_callback(input_entry):
print("User entered : " + input_entry.get())
return None
####################### GUI ###########################
root = tk.Tk()
root.geometry('300x150') #Set window size
# Heading
heading = tk.Label(root, text="A simple GUI")
heading.place(x = 100, y = 0)
input_label = tk.Label(root, text="Enter some text")
input_label.place(x = 0, y = 50)
input_entry = tk.Entry(root)
input_entry.place(x = 100, y = 50)
submit_button = tk.Button(root, text = "Submit", command = lambda: submit_callback(input_entry))
submit_button.place(x = 200, y = 90)
root.mainloop()
#############################################################

Developing a GUI is a big project for python beginners, there are several possibilities to do this. If you want to seriously develop GUI applications in Python I would recommend you to try Qt4 or Qt5 via pyside or pyqt. You may need one or more tutorials and maybe some problems to get your first working GUI applications, but you will be able to build any kind of professional cross-platform applications using this libraries.
With running command line text, you mean system commands or python commands? If you want to run system commands, I would recommend you to write a short python script, that handles user input (within the python commandline) and passes it to the system using subprocess (from subprocess import call).
If you have done your first simple textform in pyqt and the script that handles user input try to connect them by wrapping the Qt application around the commandline script. If you just looking for a quick and dirty solution there are several libraries, that support some easy to setup GUI frames or webinterfaces (to run in the browser on the local machine). But if you are a programming beginner I would highly recommend to split this into twor or three minor projects, to keep the frustration level low ;).
Edit Python2 vs Python3: pyqt and pyside are available for both python2 and python3 (as the most, but not all libraries) so your choice between py2 and py3 is on your own. The Syntax is almost the same (except the print() command), but the libraries you install are only working in the version you installed them.
If you are working on a linux machine you can easily install both versions in parallel if you want to make sure the right version is called you can specify the command such as python2 or python3 instead of running the default with python
Edit2 handle user input:
from subprocess import check_output
def predefined_command(user_input):
command = ['net', 'user', '/domain', user_input]
answer = check_output(command, args)
decoded = answer.decode('utf8')
return answer

Related

Having problems when starting a .R script using a button in a tkinter GUI

I'm currently working on a GUI for a R script I have written earlier. Since I have some experience in Python I decided to use tkinter for that.
The GUI should mainly consist of a few entry-options and a "start analysis" button. So far no problems.
I have written a function called call_r() which starts the R script using subprocess.run(...), the arguments will later come frome the users entries:
def call_r():
r_script = 'C:/Program Files/R/R-4.0.5/bin/Rscript.exe'
path_to_file = 'C:/.../Automated Analysis'
r_file = path_to_file + '/data_analysis.R'
args = ['C:/.../test/files', 'C:/.../test/results',
'2', '0.5', '3.5', '3', '5']
call = [r_script, r_file] + args
subprocess.run(call, shell=True)
If the function is called by itself it works completely fine (the .R script is executed and does its job), but as soon as I connect the function to a TkButton the R script opens, but can't import the libraries:
Error in library(ggplot2) : there is no packages called 'ggplot2'
I guess R does not find the libraries anymore...
This is the very basic tkinter GUI (just for testing reasons):
root = tk.Tk()
root.title("Automated Data Analysis")
root.geometry("50x50")
button = tk.Button(master=root, text="Run Analysis", command=call_r)
button.pack()
root.mainloop()
The problem does not occur if call_r() is executed without the tkinter GUI.
Can someone explain what is going on and how to fix this?
Update:
After trying a few different things I was able to narrow down the problem.
As soon as a tkinter function is called in the Python script (e.g. root = tk.Tk() ), the .libPaths() function in R which returns the directory of the installed packages does not work properly anymore.
It returns just one, instead of two directories.
Hope that is somehow useful information...

Is possible to have the console active only on certain times after compiling the py to exe using pyinstaller or anything else?

I’m building a program in python that will copy large files using robocopy. Since the gui freezes while the copy is done i only have two options:
1. Learn how to do multithreading and design the gui to show the progress and not freeze.
2. Keep the console on after compiling with pyinstaller as an alternative to show robocopy progress while the gui freezes.
I am open to doing multithreading but i’m a beginner and is pretty hard to understand how to make another subprocess for robocopy and from there extract the progress into a label from gui. The option i thought about is to have the cmd console active only while the copy is done. Is it possible? The scenario will be like this:
Open the program (the console will be hidden)
Press the copy button (console pops up and shows the copy progress while the gui freezes)
After the copy is done hide the console again
As i said above. I’m not totally excluding adding multithreading but for that i will need some help.
Thanks!
Please try this code, should be working, please let me know if something wrong:
import tkinter as tk
import os
import subprocess
import threading
main = tk.Tk()
main.title('Title')
frame_main = tk.Frame(main)
frame_main.grid(columnspan=1)
src = 'D:/path/to/the/folder'
dest = 'D:/path/to/the/folder2'
selection_platf = len(os.name)
def copy_build_button():
if selection_platf < 11:
subprocess.call(["robocopy", src, dest, r"/XF", 'BT V_SyncPackage.zip', "/S"])
else: #for linux
subprocess.call(["robocopy", src, dest, "/S"])
def copy_thread():
thread_1 = threading.Thread(target=copy_build_button)
thread_1.start()
button_main1 = tk.Button(frame_main, text="copy_build_button", width=50, height=5, fg="green", command=copy_thread)
button_main1.grid(column=0, sticky='N'+'S'+'E'+'W')
main.mainloop()

Tkinter w/ Python 2.7.10: Why does my GUI ignore event callbacks until a second event occurs?

Currently, I'm working with Python 2.7.10, specifically the Tkinter module, to create a GUI (I'm relatively new to Tkinter). As such, while my goal is to have the GUI print figures with matplotlib, I'm currently just testing the basics with print() to my iPython console (in Spyder). Anyways, a simple version of my code follows:
import Tkinter as tk
def run_file(i,f):
print 'running...'+`i`+', '+`f`
def select_all():
print('run')
run_file(1,97)
root = tk.Tk()
root.geometry('700x100')
singleframe = tk.Frame(root, bd = 5)
singleframe.pack()
button_fig = tk.Button(singleframe, text='Display All Figures', fg =
'black', padx = 2, command = select_all)
button_fig.pack(side = tk.LEFT)
root.mainloop()
Here's my problem:
Whenever I click my 'Display All Figures' button, the console remains blank 1, however, if I click it a second time, it displays 'run', and 'running...1, 97' (for my first click), and then displays 'run' (for the second click) 2, waiting for me to click it a third time (where it will display the second click's 'running...1, 97' result along with the third click's 'run'). Finally, if I close the program, it will display the 'running...1, 97' from my third click. (My apologies for not including more pictures; somehow I apparently have reputation below 10)
I've been trying to search the web for similar problems; several posts suggest that problems like this occur when too many calculations are occurring, so the GUI freezes up, but my function is very simple, so that seems unlikely; still others suggest the after() function, but I don't see how creating timed delays would help either. Does anyone have any suggestions as to how to solve this problem?

Tkinter OptionList gets messed up on Ubuntu

I am currently working on a User Interface using TKinter on Python 2.7.12. I started out using Windows 10 where everything went fine. Now that I transfered the code to Ubuntu (which will be the target platform) I am having trouble with an OptionList Widget.
The code for the List looks the following:
...
optionList = ['None']+self.aPandasDataFrame.index.tolist()
omv = StringVar(master=aPopUpwindow)
omv.set("None")
self.om = OptionMenu(aPopUpWindow, omv, *optionList, command = update_aPopUpWindow)
self.om.config(width=15)
self.om.place(x=20,y=20,width=400)
self.om.grid(row=1, column=0)
...
The list seems to be too large for Ubuntu to display. When I click on it, only the silhouette of an OptionList appears without text. By clicking into it, the program behaves as if I actually did a selection. The only way to currently solve this, is to drastically remove items from the list, which is no option for the final program.
Do you have any idea how to make this work?
Thanks for your help.
Sarem

How to include a command line interface in a gui in python

I'm currently building an console application in python that does a lot of calculations on input gotten from the user and i've seen several apps that have gui windows but the input is still gotten from the console and this is exactly what i want, i'm currently learning tkinter so i wanted to ask is it possible to do this in tkinter because the tutorial i have on tkinter dosen't talk about including consoles in gui's here is a screenshot of what i mean https://www.imageupload.co.uk/image/ZEXe though in the screenshot the command line just prints out what is going on in the program but i want my main application to be in that area, so i can get input from the user and then the results can be printed out on the gui. Answers can also suggest other python gui frameworks. Thanks
Tkinter can accept text inputs and return values based on what is put in. As far as I know there is no actual command line input function. You could always try implementing it yourself. I think this is very customizable so I'm not going to write it myself but these are some of the things u might use:
textbox = Entry(root)
textbox.pack()
textbox.config(bg="red") #this would change the background to black. fg for text color
textarea = Text(root) #this creates a big text area
#textbox.get() will return the value of the textbook
Also, you could embed a full terminal into a tkinter window by using xterm (you might need to install it first if it's not natively available on your machine.
from tkinter import *
import os
root = Tk()
termf = Frame(root, height=500, width=500)
termf.pack(fill=BOTH, expand=YES)
wid = termf.winfo_id()
os.system('xterm -into %d -geometry 100x100 -sb &' % wid)
root.mainloop()
This creates a new tkinter window (500 x 500) and them embeds an xterm window into it (100 x 100)

Categories