How do I require Tkinter with distutils? - 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

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

Cant Use Tkinter module in setuptools for python2 [duplicate]

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.

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?

Implementing PyObjc NsObject Subclass

I have an Objc-C project in which I would like to be able to process some data trough Python. For that, I decided to implement a PyObjc NSObject subclass, which I would then link to my Obj-C classes trough the interface builder.
I added the Python framework to my project, and an python file with the following simple code:
import objc
from Foundation import *
from AppKit import *
class PythonWrapper(NSObject):
def applicationDidFinishLaunching_(self, sender):
NSLog("Application did finish launching.")
I created a PythonWrapper instance in my XIB (the builder automatically recognized the name of it). But, when building, even without having linked it already to the other classes, I get the following issue:
Ignoring file /Developer/SDKs/MacOSX10.6.sdk/Library/Frameworks//Python.framework/Python, missing required architecture x86_64 in file
Also, the xCode log also shows:
Unknown class 'PythonWrapper', using 'NSObject' instead. Encountered in Interface Builder file at path /Users/joao/Library/Developer/Xcode/DerivedData/Jalioneiro-ekjwzbkqqgpyekadkyebhgdsjcxo/Build/Products/Debug/Jalioneiro.app/Contents/Resources/en.lproj/Interface.nib.
What am I missing here? Is there any other way to link python code to my Obj-C classes?
Note: I'm working on xCode4
I have heard that Xcode 4 does not play so nicely with PyObjC, but I'm still using 3, so I can't be sure.
The reason for your second error (and this bites me too all the time) is that you need to import your custom classes into your project's main.py:
#import modules required by application
import objc
import Foundation
import AppKit
from PyObjCTools import AppHelper
# import modules containing classes required to start application and load MainMenu.nib
import MyAppDelegate
import MyCustomView
import MyArrayController
# pass control to AppKit
AppHelper.runEventLoop()
The first error looks like you're trying to build a 64-bit-only application. The PyObjC that shipped with Snow Leopard is only compiled as 32-bit (actually, I think the same is true of the default Python). Try switching your Target's build architecture to either 32-bit or 32/64-bit Universal. You could also go down the path of re-compiling PyObjC and including it in your app's bundle -- haven't tried it myself.

Categories

Resources