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.

107 lines
3.5KB

  1. """
  2. tk_widgets.py - some simple widgets generated by Tcl/Tk
  3. """
  4. import os
  5. import sys
  6. from tkinter import Tk, messagebox, simpledialog, filedialog
  7. class SimpleWidgets:
  8. """ Simple widgets """
  9. def __init__(self):
  10. app_name = os.path.basename(sys.argv[0])
  11. base, ext = os.path.splitext(app_name)
  12. if ext.lower() in ('.py', '.pyw', '.exe'):
  13. app_name = base
  14. if sys.platform.startswith("darwin"):
  15. # Set app name, if PyObjC is installed
  16. # Python 3: pip3 install pyobjc-framework-Cocoa
  17. try:
  18. from Foundation import NSBundle
  19. bundle = NSBundle.mainBundle()
  20. if bundle:
  21. app_info = bundle.localizedInfoDictionary() or bundle.infoDictionary()
  22. if app_info:
  23. app_info['CFBundleName'] = app_name
  24. except ImportError:
  25. pass
  26. self._root = Tk()
  27. self._root.withdraw()
  28. self._app_name = app_name
  29. def alert(self, title, text):
  30. """ show alert """
  31. if title is None:
  32. title = self._app_name
  33. messagebox.showerror(title, text)
  34. def info(self, title, text):
  35. """ show info """
  36. if title is None:
  37. title = self._app_name
  38. messagebox.showinfo(title, text)
  39. def get_text(self, title, label, default=""):
  40. """ get text """
  41. if title is None:
  42. title = self._app_name
  43. val = simpledialog.askstring(title, label, initialvalue=default)
  44. return val
  45. def get_int(self, title, label, default=0,
  46. min_val=-2147483647, max_val=2147483647):
  47. """ get integer """
  48. if title is None:
  49. title = self._app_name
  50. val = simpledialog.askinteger(title, label, initialvalue=default,
  51. minvalue=min_val, maxvalue=max_val)
  52. return val
  53. def get_float(self, title, label, default=0.0,
  54. min_val=-2147483647.0, max_val=2147483647.0):
  55. """ get float """
  56. if title is None:
  57. title = self._app_name
  58. val = simpledialog.askfloat(title, label, initialvalue=default,
  59. minvalue=min_val, maxvalue=max_val)
  60. return val
  61. def get_open_filename(self, title, start_dir="", filters=None):
  62. """
  63. get filename to open
  64. filters is a sequence of (label, pattern) tuples.
  65. """
  66. if title is None:
  67. title = self._app_name
  68. start_dir = os.path.expanduser(start_dir)
  69. if filters:
  70. val = filedialog.askopenfilename(title=title, initialdir=start_dir,
  71. filetypes=filters)
  72. else:
  73. val = filedialog.askopenfilename(title=title, initialdir=start_dir)
  74. if not val:
  75. val = None
  76. return val
  77. def get_save_filename(self, title, start_dir="", filters=None):
  78. """
  79. get filename to save
  80. filters is a sequence of (label, pattern) tuples.
  81. """
  82. if title is None:
  83. title = self._app_name
  84. start_dir = os.path.expanduser(start_dir)
  85. if filters:
  86. val = filedialog.asksaveasfilename(title=title,
  87. initialdir=start_dir,
  88. filetypes=filters)
  89. else:
  90. val = filedialog.asksaveasfilename(title=title,
  91. initialdir=start_dir)
  92. if not val:
  93. val = None
  94. return val