Source code for mytk.labels
import tkinter.font as tkFont
import tkinter.ttk as ttk
from tkinter import StringVar
from .base import Base
[docs]
class Label(Base):
"""A text label widget wrapping ttk.Label with optional word wrapping."""
def __init__(self, text=None, wrapping=False, **kwargs):
Base.__init__(self)
self.wrapping = wrapping
self._widget_args = kwargs
if text is None:
self.text = ""
else:
self.text = text
self.value_variable = StringVar()
self.bind_properties(
"text", self, "value_variable"
) # binding this way will set value_variable to 'text'
[docs]
def set_label_wrap(self, event):
"""Adjust the label wrap length to fit the current widget width."""
wraplength = event.width - 12 # 12, to account for padding and borderwidth
event.widget.config(wraplength=wraplength)
[docs]
class URLLabel(Label):
"""A clickable label that opens a URL in the default web browser."""
def __init__(self, url=None, text=None):
super().__init__(self, text)
self.url = url
if text is None:
text = url
self.text = text
[docs]
def open_url(self):
"""Open the associated URL in the default web browser."""
try:
from webbrowser import open_new_tab
open_new_tab(self.url)
except ModuleNotFoundError:
print(
"Cannot open link: module webbrowser not installed. Install with `pip3 install webbrowser`"
)