Cant Use Tkinter module in setuptools for python2 [duplicate] - python

I'm trying to compile a program using distutils but I want to make sure that the user has Tkinter installed before installing my package.
My Google searches have failed to turn up any useful info, any clue how I'd do this?
Thanks,
Wayne

You can have a class that inherits from install and then do this:
from distutils.command.install import install
class Install(install):
def run(self):
if not check_dependencies():
# Tkinter was not installed, handle this here
install.run(self) # proceed with the installation
def check_dependencies():
try:
return __import__('Tkinter')
except ImportError:
return None

Unfortunately there is no standard cross-platform way to force Tkinter to be installed. Tkinter is part of the Python standard library so distributors who strip out Tkinter, or other standard library modules, and package them as optional entities are doing so using their own package management tools and, in general, you'd need to know the specific commands for each distribution. The best you can do in general is test for and fail gracefully if Tkinter (or tkinter in Python 3) is not importable, so something like:
import sys
try:
import Tkinter
except ImportError:
sys.exit("Tkinter not found")

Tkinter is in the python standard library, it should always be there.

Related

Python turtle: problems with tkinter

So I wanted to use the python package turtle, which uses the underlying package tkinter.
With this code:
import turtle
python raised an error which said, that the package tkinter was not found.
So I edited the turtle.py file and changed the
from tkinter import xyz
to
from tk import xyz
and at least the error was gone, but another appeared.
It is now saying
ImportError: cannot import name 'simpledialog' from 'tk' (C:\PYTHON\lib\site-packages\tk\__init__.py)
I've updated pip and reinstalled both packages.
I'm using python 3.9.1 on Windows.
Try using:
from tkinter.simpledialog import askstring
Or whichever type of simpledialog you want to make
Feel free to ask question in case of confusion.

windows tkinter install python 3.7.4

installed PYTHON 3.7.4 from the python.org website. Working on a windows 10 machine. everywhere it says tkinter should be part of it but it seems like it's not. I need help installing it.
This is the official documentation for the standard library Tkinter created by the folks at Python Software Foundation :
Tkinter
This will definitely help you install, import and get started with Tkinter on your Windows system.
For importing errors while trying to use Tkinter, please note :
The root of the problem is that the Tkinter module is named Tkinter (capital "T") in python 2.x, and tkinter (lowercase "t") in python 3.x.
To make your code work in both Python 2 and 3 you can do something like this:
try:
# for Python2
from Tkinter import *
except ImportError:
# for Python3
from tkinter import *
But since you've already mentioned that you're on Python 3.7.4, this import statement will suffice :
import tkinter as tk
Tkinter is built in with every Python installation.
I don't know if you typed the command wrong, but this is the proper way.
import Tkinter as tk
Note: There is a Capital T at the beginning of Tkinter.
In Python 3 for you,
import tkinter as tk
Note: You might accidentally deleted it in the folder of python so you could just download python again.
Below from https://pypi.org/project/PySimpleGUI/:
Python 3.7
If you must run 3.7, try 3.7.2. It does work with PySimpleGUI with no known issues.
PySimpleGUI with Python 3.7.3 and 3.7.4+. tkinter is having issues with all the newer releases. Things like Table colors stopped working entirely. It's unclear if there's been a change that makes the tkinter API no longer working which means they are not backwards compatible. That would be a real shame. If so, more work needs to be done in PySimpleGUI
Can you import tkinter by import tkinter?

No module named Tkinter error

I have been using http://python.codnex.net/index.php. I have tried to use Tkinter but it shows ImportError: No module named Tkinter on line 1
I have tried many things including:
import Tkinter
import tkinter
from tkinter import *
If you find something please let me know.
According to this question & answer in Quora you can't use tkinter with online interpreters. Though apparently you can run linux along chromeOS if you have non-school version.
I might be wrong, but I believe it's because tkinter is a GUI module, which can't be ran from a webbrowser.
Have you tried importing it in a local python instance?
it wont work on web browser ,Run a local python instance and import tkinter after installing tkinter by using command prompt using the command pip install python-tk

How do i import gtk module to my application? python

dialog = gtk.FileChooserDialog("Open..",
None,
gtk.FILE_CHOOSER_ACTION_OPEN,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_OPEN, gtk.RESPONSE_OK))
dialog.set_default_response(gtk.RESPONSE_OK)
I want to use the above code for file browsing- but when I give the following import
import pygtk
pygtk.require('2.0')
import gtk
I get Error: No module named 'pygtk'
Do I need to give pygtk as a module inside my application folder? Do say with steps. Thank you.
pygtk is the old and deprecated gtk Python api. Do not use it unless you have a legacy application. The correct way nowadays is to use GObject-introspection to use Gtk3 from Python. Which is also really awesome as it makes Python a first-class supported language, with no wrappers necessary.
from gi.repository import Gtk
dlg = Gtk.FileChooserDialog()
dlg.show()
You should look at this: http://rox.sourceforge.net/desktop/node/245.html
It shows some problems. Good luck on fixing it :).
Edit:
This has lots of info on pip install (windows): How do I install pip on Windows?

How do I require Tkinter with distutils?

I'm trying to compile a program using distutils but I want to make sure that the user has Tkinter installed before installing my package.
My Google searches have failed to turn up any useful info, any clue how I'd do this?
Thanks,
Wayne
You can have a class that inherits from install and then do this:
from distutils.command.install import install
class Install(install):
def run(self):
if not check_dependencies():
# Tkinter was not installed, handle this here
install.run(self) # proceed with the installation
def check_dependencies():
try:
return __import__('Tkinter')
except ImportError:
return None
Unfortunately there is no standard cross-platform way to force Tkinter to be installed. Tkinter is part of the Python standard library so distributors who strip out Tkinter, or other standard library modules, and package them as optional entities are doing so using their own package management tools and, in general, you'd need to know the specific commands for each distribution. The best you can do in general is test for and fail gracefully if Tkinter (or tkinter in Python 3) is not importable, so something like:
import sys
try:
import Tkinter
except ImportError:
sys.exit("Tkinter not found")
Tkinter is in the python standard library, it should always be there.

Categories

Resources