Tkinter OptionList gets messed up on Ubuntu - python

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

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

GUI Interface For Command Line Input/Output

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

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 menu bindings and accelerators

I've come across a weird problem and I can't work out what is happening. I am working on a Tkinter application which utilises a menu. I have found that while creating the menu, in which all items have accelerators, some items require additional bindings to make the accelerators work (unless the menu bars are already selected) and some don't.
When an additional binding is required, I have a problem with double entry key strokes. For example, if I open a Toplevel window, I get double entries every time I type a character in an Entry box, both in the Toplevel and in the main window. This only happens if the menu item is called via the key command.
Most of the time this is not a problem, although I'd really like to know what the underlying cause is because it just seems wrong, but this particularly came to my attention recently when I implemented the built-in OS X Preferences menu, using the following code:
self.window.createcommand('::tk::mac::ShowPreferences', self._settings)
Now when I call the settings function from the build-in Preferences key command Command-,, which instantiates a new Toplevel window, this double entry is what happens. It does not happen if I navigate to the menu and open it with the mouse.
The example below recreates the problem for me. The menu bar is not strictly necessary, but the problem occurs with both the menu bar and the OS X built-in Preferences item. Interestingly, 'Settings A' which does not require a binding reproduces the problem, but 'Settings B', which does require a binding, works fine. And again, only with key commands.
import Tkinter
def settings(event = None):
top = Tkinter.Toplevel()
Tkinter.Entry(top).pack()
top.mainloop()
root = Tkinter.Tk()
root.createcommand('::tk::mac::ShowPreferences', settings)
menuBar = Tkinter.Menu(root)
fileMenu = Tkinter.Menu(menuBar)
fileMenu.add_command(label = 'Settings A', accelerator = 'Command-Shift-a', command = settings) # Does not require binding
fileMenu.add_command(label = 'Settings B', accelerator = 'Command-b', command = settings) # Requires binding
menuBar.add_cascade(label = 'File', menu = fileMenu)
root.config(menu = menuBar)
root.bind('<Command-b>', settings)
Tkinter.Entry(root).pack()
root.mainloop()
I suspected it may be a computer issue but I have tried it on another machine and I get the same result. Does anybody have any idea what is happening here and how I can prevent it?
In case anybody's interested, I think I've got to the bottom of this. I believe it was caused by the version of tkinter I had. Today I updated to Python 3 from the OS X bundled version 2.7, and the problem remained. Then I updated tkinter to ActiveTcl 8.5.18.0 and the problem seems to have disappeared.
IDLE and tkinter with Tcl/Tk on macOS

Categories