How do you fix tkinter in python3.8 on wsl? - python

I was trying to learn GUI based python therefore i was using the Tkinter library. My OS is windows but I have installed Ubuntu wsl as my default terminal, and use wsl vscode as my default text-editor.
I was just creating a basic window using this sample code to check whether it works or not:
from Tkinter import *
def onclick():
pass
root = Tk()
text = Text(root)
text.insert(INSERT, "Hello.....")
text.insert(END, "Bye Bye.....")
text.pack()
text.tag_add("here", "1.0", "1.4")
text.tag_add("start", "1.8", "1.13")
text.tag_config("here", background="yellow", foreground="blue")
text.tag_config("start", background="black", foreground="green")
root.mainloop()
Turns out it does not work with python3.8.
Terminal error message
I looked up multiple resources and forums and could not find any proper solutions. Here are couple of links i have referred to:
https://realpython.com/python-gui-tkinter/ https://tkdocs.com/tutorial/install.html https://askubuntu.com/questions/1224230/how-to-install-tkinter-for-python-3-8 .
A work around I discovered was that installed anaconda which uses python3.7 and used the windows terminal instead (using pycharm not vscode). And it looks like it is functioning properly.
GUI with python3.7
The thing is.. I like wsl and vscode far better than windows command line, and I wanted to revert back to it. Is there any solution? I really do not want to fool around with the path too much because I had a bad experience previously (But i am willing to do it again if its going to fix my problem). Thanks for hearing me out.

The problem isn't Python, but WSL. You can't run anything graphical easily inside WSL. (Microsoft is planning to add that feature, but it isn't ready yet. There are third-party solutions, but they're not that easy to set up.)
The easiest solution is to use a Windows install of Python 3.8 to run your tkinter app. You can still invoke that from inside your Ubuntu WSL, same as any Windows executable.

WSL now supports graphical interfaces. In order to use it you need to install or update WSL more info. Then run your app as usual and your program window should open.

Related

How to launch Linux elevated privileges window from Python?

I am writing a python3 app, running with a GUI (not in the terminal) as the user. While this app is running as the user, I need to also execute a command that requires root privileges. I've seen numerous examples of how to do this in the terminal, like this one, which does in fact work great in the terminal:
import subprocess
subprocess.call('sudo echo "Hello world!"', shell=True)
...but my python app is not running in the terminal, and this method doesn't seem to automatically provoke a visible system prompt when my GUI code calls it.
Other apps, notably the Snap Store, somehow launch what appears to be a system popup when they need to do something with root privileges. For example, when I use the Snap Store to install Notepad++ on Ubuntu, I get this popup, which I suspect is a system popup rather than something the Snap Store folks custom-designed.
I'm not super familiar with Linux systems, but I assume this is not a custom popup instead of a system popup because who wants to give their root password to the makers of one of their apps? Instead, users would be giving it to the system (which already knows it), and the 3rd-party app itself doesn't have access to the actual password, but can now run a command as root.
How can I launch an identical popup from my Python app in order to run a command that requires root privileges? Or, alternatively, how else can I run a root command from my python GUI running as the user?
Thanks in advance for any clear assistance you can provide!
Have you tried pkexec?
Not sure about your GUI but running a simple python script (python3.8, pycharm, ubuntu 18.04) results in the popup you desire (can't do the screen print of the pop-up).
import os
os.system("pkexec ls -l")

python auto install modules and pygame window

I have two questions about pygame and python:
One: It is using pygame in fullscreen mode and I was wondering how I can get other windows (google chrome, Safari, Mail, Thunderbird, Itunes) to go on top of the already fullscreen window. Two: Can I make Python install all the modules that the custom desktop will need without the user having to go and install them all themselves
Just make a borderless screen that is the size of your desktop excluding the start menu etc. For the second question, just compile so the user doesn't need python. I have done this quite easily with py2exe, assuming you are running windows, I have heard py2app is good for mac, py2exe cant really be used easily on linux but with wine its programs seem to run fine.

Apple Python launcher is acting on command key bindings

I've written a Python utility that uses tkinter. I'm running it on a Macintosh. When it is executed, it runs within an apple-supplied Python launcher program (/Library/Frameworks/Python.framework/Versions/3.2/Resources/Python.app).
My code installs its own menus and I bind to the usual Macintosh command-key equivalents for my edit menu (Command-x, command-c, command-x, command-a, command-z) and for quitting (command-q). My problem is that the Python launcher program is responding to the command key bindings. This is inconvenient for things like pasting because it gets done twice. It's a real problem with quitting because the launcher program kills my program before I can save changed files.
Is there some way I can stop the Python launcher program from acting on command key equivalents? I attempted this: "rootWindow.unbind ('<Command-Key-q>')", but to no avail. The launcher program quits before my code can clean up.
I'm using CPython 3.2 on OS X 10.6.6.
Instead of overriding Tkinter's default key bindings, consider re-mapping Tcl's "exit" command to a custom function. (This is called every time you hit command-q or use the "quit" menu item.)
def save_and_exit():
save_changed_files()
sys.exit()
self.createcommand('exit', save_and_exit)
Besides that, I would recommend removing your copy/paste custom keybinds and letting the library do the work for you. If you're still hell-bent on overriding the defaults, Effbot has a nice tutorial on Tkinter events and bindings.
Is there a specific reason why you are using Python.app for launching? This .app is most likely the reason for misbehaving shortcuts.
If I have understood correctly, this launcher is just a wrapper for default python (/usr/bin/python) with special imports.
If you run from terminal (-v is the key here):
/Library/Frameworks/Python.framework/Versions/5.1.1/Resources/Python.app/Contents/MacOS/Python -v
You will see what it imports at the beginning. Adding these lines to your main file should make the command line launching the same as with the .app.
Note also that python.app is in version 5.1.1.
br,
Juha
First off, /Library/Frameworks/Python.framework/Versions/3.2/Resources/Python.app is not Apple-supplied. Most likely you installed Python 3.2 using one of the python.org installers here or from some third-party distributor or possibly you built a framework version from source. In any case, Python.app is a dummy application bundle included in each framework version. Its purpose is to ensure that when you invoke python, even from a command line, it is seen by OS X as a full-fledged GUI application. This is particularly important when using tkinter. The default menus and keybindings you see are supplied by Tcl/Tk, not tkinter. As you've discovered, the right way to go about changing these are to remap the default menus. Be aware that there are currently at least three major variants of Tk available on Mac OS X: Aqua Carbon Tk, Aqua Cocoa Tk, and X11 Tk. There are important details, especially with regards to Mac OS X 10.6, about Python and Tcl/Tk on Mac OS X at the python.org website.

wx.App (wxPython) crash when calling

Recently I installed wxPython to do some works under Windows. Most of the time I work in Linux so I have a little experience here.
with python.exe interpreter, I just do 2 line of codeimport wxtmp=wx.App(False)
Then the interpreter crashed with Windows error reporting.
I tried both python 2.7.1 and 2.6.6 with wxPython 2.8.11, all come from their main website, still no luck.
Is there something I must do after install Python in Windows ? I can see that python install just fine and can do some basic job, wxPython library can be load, but can't call wx.App
If you are running that in IDLE, then that is your problem. IDLE and wx don't get along very well because you basically end up with two mainloops fighting each other. Try putting it in a file and then run the file from the command line:
c:\python27\python.exe myPyFile.py
That should work just fine. Otherwise, download the correct wxPython for your Python and OS (32/64 bit), uninstall the current one and install the new one. I've been using wxPython on Windows XP, Vista and 7 with no problems like this.
In case like me somebody will stumble into this question like I did. Recently installed wxpython on two machines, windows 7 and XP. Testing the sample code in simple.py (provided with the wxpython docs-demos installer), running from a python console, I had the following problem on both machines: First import ok, but when I did a reload of the module, python crash.
I added this line in the end of the simple.py file: del app
and that fixed the problem on windows 7 and tomorrow I try it on the XP machine.
Same solution fitted for the XP machine. So, reloading an un-edited module with a reference to a wx.App with a closed gui seem not to be feasable. Killing the reference with a del statement was enough to solve the problem.
I searched for a while and found that this is the problem with wxPython and Python >2.5. Tried many fix with manyfest file but no luck, so I think switch to PyQt is the only solution now.

Hide console for Tkinter app on OSX

I'm trying to hide the Terminal when I launch a GUI Tkinter based app, but when I double click the app.py file on OSX, the Terminal window appears. I've tried changing the extension to .pyw and tried launching it with /usr/bin/pythonw, but no matter what, the Terminal window still appears.
I've even tried adding the try/except below, but when I run it I get the error: 'invalid command name "console"' in the Terminal window that appears.
from Tkinter import *
class MainWindow(Tk):
def __init__(self):
Tk.__init__(self)
try:
self.tk.call('console', 'hide')
except TclError, err:
print err
win = MainWindow()
win.mainloop()
I haven't been able to find any way to hide the Terminal window from appearing. Anybody got any ideas?
By double-clicking a .py file on OS X, you are likely launching a Python gui instance via the Python Launcher.app supplied with OS X Pythons. You can verify that by selecting the .py file in the Finder and doing a Get Info on it. Python Launcher is a very simple-minded app that starts Python via a Terminal.app command. To directly launch your own Python GUI app, the preferred approach is to create a simple app using py2app. There's a brief tutorial here.
EDIT:
There are other ways, of course, but most likely any of them would be adding more levels of indirection. To make a normal launchable, "double-clickable" application, you need some sort of app structure. That's what py2app lets you create directly.
A very simple-minded alternative is to take advantage of the AppleScript Editor's ability to create a launcher app. In the AppleScript editor:
/Applications/Utilities/AppleScript
Editor.app in OS X 10.6
/Applications/AppleScript/Script
Editor.app in 10.5
make a new script similar to this:
do shell script "/path/to/python /path/to/script.py &> /dev/null &"
and then Save As.. with File Format -> Application. Then you'll have a double-clickable app that will launch another app. You can create something similar with Apple's Automater.app. But, under the covers, they are doing something similar to what py2app does for you, just with more layers on top.
Adding to the answer by Ned Deily, im my case when I tried to launch the Python application using an AppleScript application, it did not work initially. I discovered that it has something to to with some kind of encoding error (I am using UTF-8 and in the past I had felt need to configure it to UTF-8).
So, after further investigation, I discovered that I can accomplish this by creating an AppleScript application with the following code (adjusting the paths of python3 and of the Python application as needed):
do shell script "export LC_ALL=en_US.UTF-8; export LANG=en_US.UTF-8; /usr/local/bin/python3 '/Users/USER/FOLDER/SCRIPT.py' &> /dev/null &"
It launches the Python application without any Terminal windows. The AppleScript application can then be personalised with a custom icon as usual, and can be placed in the Dock. When clicked, it will launch the Python intepreter, that still shows up in Dock, but with no visible windows.
I think this may be useful to other users.
'console hide' doesn't hide the Terminal in OS X. It hides Tk's built-in console, which is really a relic from the MacOS Classic days (and which is still commonly used on Windows).

Categories