Skip to main content

Odd tkinter utilities, including image menu button

Project description

python_tk_oddbox

A few odd tkinter utilities.

Basic Description

  • Images class--supports PhotoImages organized by file stem names they were loaded from.
  • ImageMenuButton--uses images so loaded to make an image based menubutton/menu with an associated StringVar control variable. The user can select an image and its name will be set in the StringVar and displayed on the button. If the StringVar is set, the image displayed on button will change automatically--but it should always be a name of an image in Images.

CAVEATS

  1. The package is likely too specific in focus to be useful to many.
  2. Backwards compatibility is not expected to be maintained.
  3. This code is probably never going to be production grade and very little maintenance is anticipated.

Example Usage of Images class

This example assumes the following directory structure

/directory/path
    lion.jpg
    tiger.jpg
    bear.gif
    some.txt

The following code will load lion, tiger, and bear as PhotoImages that are accessible as Images.bear and Images.flower. The some.txt file is ignored because we don't load the directory with ".txt". Then it will create a menubutton that will select between lion, tiger, and bear based on images. The value of the text variale animal will be bound to this menubutton such that changing it will change the image on the button and selecting an image will change the text variable.

from tkinter import Tk, Label, StringVar
from tk_oddbox import Images, ImageLoader, ImageLabel, ImageMenuButton

tk = Tk()

loader = ImageLoader(tk)

loader.load_dir("/directory/path", "*.jpg")
loader.load_dir("/directory/path", "*.gif")

lion_image_1 = Images.lion
lion_image_2 = Images["lion"]
assert lion_image_1 is lion_image_2
assert "lion" in Images


animal_var = StringVar()
animal_var.set("lion")
animal_choices = ["lion", "tiger", "bear"]

animal_label = ImageLabel(tk, animal_var)
animal_menu = ImageMenuButton(tk, animal_var, animal_choices) 

animal_menu.grid(row=0, column=0)

tk.mainloop()

The expressions like "*.gif" are glob expressions and are passed to the Path.glob() method in the pathlib module.

Caveats:
  • Files are keyed by their stem name, so care should be used to avoid name collisions.
  • File stem names starting with underscores or which are not valid python identifiers should be avoided.
  • StringVar control variable associated with an ImageMenuButton should never be set to something other than a name in Images. This means an Entry widget with this StringVar is probably a bad idea.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tk-oddbox-0.0.2.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tk_oddbox-0.0.2-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file tk-oddbox-0.0.2.tar.gz.

File metadata

  • Download URL: tk-oddbox-0.0.2.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.3

File hashes

Hashes for tk-oddbox-0.0.2.tar.gz
Algorithm Hash digest
SHA256 45618c31df9bfa5ae48b5e7d8ee98852803dd8ae48b6541781c1afb34819be85
MD5 b02b93c07e7f2a08f8c4a3e537a84e3e
BLAKE2b-256 bd2fc56029d2746f89648d586a85cdce40a8a02ae1c4d9b52092d74bee6a928a

See more details on using hashes here.

File details

Details for the file tk_oddbox-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: tk_oddbox-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.3

File hashes

Hashes for tk_oddbox-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f029a5d9c0b8abfcb0c39b99194d436d0086de097116ea6c063bf3f8a260000a
MD5 f448a82b9773770c7a5558a8f43102a5
BLAKE2b-256 d536888c845df978e29b34a89e2d0d4f67ae597d34c1a1ac9075c92981107300

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page