Collection of GNS3 GUI Tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.5 KiB

"""
tk_widgets.py - some simple widgets generated by Tcl/Tk
"""
import os
import sys
from tkinter import Tk, messagebox, simpledialog, filedialog
class SimpleWidgets:
""" Simple widgets """
def __init__(self):
app_name = os.path.basename(sys.argv[0])
base, ext = os.path.splitext(app_name)
if ext.lower() in ('.py', '.pyw', '.exe'):
app_name = base
if sys.platform.startswith("darwin"):
# Set app name, if PyObjC is installed
# Python 3: pip3 install pyobjc-framework-Cocoa
try:
from Foundation import NSBundle
bundle = NSBundle.mainBundle()
if bundle:
app_info = bundle.localizedInfoDictionary() or bundle.infoDictionary()
if app_info:
app_info['CFBundleName'] = app_name
except ImportError:
pass
self._root = Tk()
self._root.withdraw()
self._app_name = app_name
def alert(self, title, text):
""" show alert """
if title is None:
title = self._app_name
messagebox.showerror(title, text)
def info(self, title, text):
""" show info """
if title is None:
title = self._app_name
messagebox.showinfo(title, text)
def get_text(self, title, label, default=""):
""" get text """
if title is None:
title = self._app_name
val = simpledialog.askstring(title, label, initialvalue=default)
return val
def get_int(self, title, label, default=0,
min_val=-2147483647, max_val=2147483647):
""" get integer """
if title is None:
title = self._app_name
val = simpledialog.askinteger(title, label, initialvalue=default,
minvalue=min_val, maxvalue=max_val)
return val
def get_float(self, title, label, default=0.0,
min_val=-2147483647.0, max_val=2147483647.0):
""" get float """
if title is None:
title = self._app_name
val = simpledialog.askfloat(title, label, initialvalue=default,
minvalue=min_val, maxvalue=max_val)
return val
def get_open_filename(self, title, start_dir="", filters=None):
"""
get filename to open
filters is a sequence of (label, pattern) tuples.
"""
if title is None:
title = self._app_name
start_dir = os.path.expanduser(start_dir)
if filters:
val = filedialog.askopenfilename(title=title, initialdir=start_dir,
filetypes=filters)
else:
val = filedialog.askopenfilename(title=title, initialdir=start_dir)
if not val:
val = None
return val
def get_save_filename(self, title, start_dir="", filters=None):
"""
get filename to save
filters is a sequence of (label, pattern) tuples.
"""
if title is None:
title = self._app_name
start_dir = os.path.expanduser(start_dir)
if filters:
val = filedialog.asksaveasfilename(title=title,
initialdir=start_dir,
filetypes=filters)
else:
val = filedialog.asksaveasfilename(title=title,
initialdir=start_dir)
if not val:
val = None
return val