def create_file_menu(self): modifier = TkUtil.menu_modifier() self.fileMenu = tk.Menu(self.menubar, name="apple") self.fileMenu.add_command(label=NEW, underline=0, command=self.new, image=self.menuImages[NEW], compound=tk.LEFT, accelerator=modifier + "+N") self.fileMenu.add_command(label=OPEN + ELLIPSIS, underline=0, command=self.open, image=self.menuImages[OPEN], compound=tk.LEFT, accelerator=modifier + "+O") self.fileMenu.add_cascade(label=OPEN_RECENT, underline=5, image=self.menuImages[OPEN], compound=tk.LEFT) self.fileMenu.add_command(label=SAVE, underline=0, command=self.save, image=self.menuImages[SAVE], compound=tk.LEFT, accelerator=modifier + "+S") self.fileMenu.add_command(label=SAVE_AS + ELLIPSIS, underline=5, command=self.save_as, image=self.menuImages[SAVEAS], compound=tk.LEFT) if TkUtil.mac(): self.master.createcommand("::tk::mac::ShowPreferences", self.preferences) self.master.createcommand("exit", self.close) else: self.fileMenu.add_separator() self.fileMenu.add_command(label=PREFERENCES + ELLIPSIS, underline=0, image=self.menuImages[PREFERENCES], compound=tk.LEFT, command=self.preferences) self.fileMenu.add_separator() self.fileMenu.add_command(label=QUIT, underline=0, command=self.close, compound=tk.LEFT, image=self.menuImages[QUIT], accelerator=modifier + "+Q") self.menubar.add_cascade(label="File", underline=0, menu=self.fileMenu)
def create_widgets(self): self.sourceLabel = ttk.Label(self, text="Source Folder:", underline=-1 if TkUtil.mac() else 1) self.sourceEntry = ttk.Entry(self, width=30, textvariable=self.sourceText) self.sourceButton = TkUtil.Button(self, text="Source...", underline=0, command=lambda *args: self.choose_folder(SOURCE)) self.helpButton = TkUtil.Button(self, text="Help", underline=0, command=self.help) self.targetLabel = ttk.Label(self, text="Target Folder:", underline=-1 if TkUtil.mac() else 1) self.targetEntry = ttk.Entry(self, width=30, textvariable=self.targetText) self.targetButton = TkUtil.Button(self, text="Target...", underline=0, command=lambda *args: self.choose_folder(TARGET)) self.aboutButton = TkUtil.Button(self, text="About", underline=1, command=self.about) self.statusLabel = ttk.Label(self, textvariable=self.statusText) self.scaleButton = TkUtil.Button(self, text="Scale", underline=1, command=self.scale_or_cancel, default=tk.ACTIVE, state=tk.DISABLED) self.quitButton = TkUtil.Button(self, text="Quit", underline=0, command=self.close) self.dimensionLabel = ttk.Label(self, text="Max. Dimension:", underline=-1 if TkUtil.mac() else 6) self.dimensionCombobox = ttk.Combobox(self, textvariable=self.dimensionText, state="readonly", values=("50", "100", "150", "200", "250", "300", "350", "400", "450", "500")) TkUtil.set_combobox_item(self.dimensionCombobox, "400")
def create_edit_menu(self): modifier = TkUtil.menu_modifier() self.editMenu = tk.Menu(self.menubar) self.editMenu.add_command(label=UNDO, underline=0, command=self.editor.edit_undo, image=self.menuImages[UNDO], compound=tk.LEFT, accelerator=modifier + "+Z") redo = "+Shift+Z" if TkUtil.windows(): redo = "+Y" self.editMenu.add_command(label=REDO, underline=0, command=self.editor.edit_redo, image=self.menuImages[REDO], compound=tk.LEFT, accelerator=modifier + redo) self.editMenu.add_separator() self.editMenu.add_command(label=COPY, underline=0, command=lambda: self.editor.text.event_generate( "<<Copy>>"), image=self.menuImages[COPY], compound=tk.LEFT, accelerator=modifier + "+C") self.editMenu.add_command(label=CUT, underline=2, command=lambda: self.editor.text.event_generate("<<Cut>>"), image=self.menuImages[CUT], compound=tk.LEFT, accelerator=modifier + "+X") self.editMenu.add_command(label=PASTE, underline=0, command=lambda: self.editor.text.event_generate( "<<Paste>>"), image=self.menuImages[PASTE], compound=tk.LEFT, accelerator=modifier + "+V") self.editMenu.add_separator() self.editMenu.add_command(label=FIND + ELLIPSIS, underline=0, command=self.find, image=self.menuImages[FIND], compound=tk.LEFT, accelerator=modifier + "+F") self.menubar.add_cascade(label="Edit", underline=0, menu=self.editMenu)
def create_file_toolbar(self): settings = TkUtil.Settings.Data self.fileToolbar = ttk.Frame(self.toolbarFrame, relief=tk.RAISED) self.fileToolbar.text = FILE_TOOLBAR self.fileToolbar.underline = 0 menuButton = ttk.Button(self.fileToolbar, text="File Toolbar Menu", image=self.toolbarImages[TOOLBARMENU], command=self.toolbar_menu) TkUtil.bind_context_menu(menuButton, self.toolbar_menu) TkUtil.Tooltip.Tooltip(menuButton, text="File Toolbar Menu") newButton = ttk.Button(self.fileToolbar, text=NEW, image=self.toolbarImages[NEW], command=self.new) TkUtil.Tooltip.Tooltip(newButton, text="New Document") openButton = ttk.Button(self.fileToolbar, text=OPEN, image=self.toolbarImages[OPEN], command=self.open) TkUtil.Tooltip.Tooltip(openButton, text="Open Document") self.saveButton = ttk.Button(self.fileToolbar, text=SAVE, image=self.toolbarImages[SAVE], command=self.save) TkUtil.Tooltip.Tooltip(self.saveButton, text="Save Document") preferencesButton = ttk.Button(self.fileToolbar, text=PREFERENCES, image=self.toolbarImages[PREFERENCES], command=self.preferences) TkUtil.Tooltip.Tooltip(preferencesButton, text=PREFERENCES) TkUtil.add_toolbar_buttons(self.fileToolbar, (menuButton, newButton, openButton, self.saveButton, None, preferencesButton)) self.toolbars.append(self.fileToolbar)
def create_alignment_toolbar(self): settings = TkUtil.Settings.Data self.alignmentToolbar = ttk.Frame(self.toolbarFrame, relief=tk.RAISED) self.alignmentToolbar.text = ALIGNMENT_TOOLBAR self.alignmentToolbar.underline = 0 menuButton = ttk.Button(self.alignmentToolbar, text="Alignment Toolbar Menu", image=self.toolbarImages[TOOLBARMENU], command=self.toolbar_menu) TkUtil.bind_context_menu(menuButton, self.toolbar_menu) TkUtil.Tooltip.Tooltip(menuButton, text="Alignment Toolbar Menu") self.leftButton = ttk.Button(self.alignmentToolbar, text=ALIGN_LEFT, image=self.toolbarImages[ALIGNLEFT]) self.leftButton.config( command=lambda: self.toggle_alignment(tk.LEFT)) TkUtil.Tooltip.Tooltip(self.leftButton, text=ALIGN_LEFT) self.centerButton = ttk.Button(self.alignmentToolbar, text=ALIGN_CENTER, image=self.toolbarImages[ALIGNCENTER]) self.centerButton.config( command=lambda: self.toggle_alignment(tk.CENTER)) TkUtil.Tooltip.Tooltip(self.centerButton, text=ALIGN_CENTER) self.rightButton = ttk.Button(self.alignmentToolbar, text=ALIGN_RIGHT, image=self.toolbarImages[ALIGNRIGHT]) self.rightButton.config( command=lambda: self.toggle_alignment(tk.RIGHT)) TkUtil.Tooltip.Tooltip(self.rightButton, text=ALIGN_RIGHT) TkUtil.add_toolbar_buttons(self.alignmentToolbar, (menuButton, self.leftButton, self.centerButton, self.rightButton)) self.toolbars.append(self.alignmentToolbar) self.leftButton.state((TkUtil.SELECTED,)) self.alignment.set(tk.LEFT)
def populate_comboboxes(self): currencies = sorted(self.rates.keys()) for combobox in (self.currencyFromCombobox, self.currencyToCombobox): combobox.state(("readonly",)) combobox.config(values=currencies) TkUtil.set_combobox_item(self.currencyFromCombobox, "USD", True) TkUtil.set_combobox_item(self.currencyToCombobox, "GBP", True) self.calculate()
def extend(self): self.extendButton.state((TkUtil.SELECTED,)) self.extendButton.config(text="Unextend", underline=3 if not TkUtil.mac() else -1) if not TkUtil.x11(): self.extendButton.config(image=self.images[UNEXTEND]) self.title("Find and Replace \u2014 {}".format(APPNAME)) for widget in self.extensionWidgets: widget.grid()
def unextend(self): self.extendButton.state((TkUtil.NOT_SELECTED,)) self.extendButton.config(text="Extend", underline=1 if not TkUtil.mac() else -1) if not TkUtil.x11(): self.extendButton.config(image=self.images[EXTEND]) self.title("Find \u2014 {}".format(APPNAME)) for widget in self.extensionWidgets: widget.grid_remove()
def create_widgets(self, master): self.frame = ttk.Frame(master) self.usernameLabel = ttk.Label(self.frame, text="Username:"******"Password:"******"•")
def create_edit_toolbar(self): settings = TkUtil.Settings.Data self.editToolbar = ttk.Frame(self.toolbarFrame, relief=tk.RAISED) self.editToolbar.text = EDIT_TOOLBAR self.editToolbar.underline = 0 menuButton = ttk.Button( self.editToolbar, text="Edit Toolbar Menu", image=self.toolbarImages[TOOLBARMENU], command=self.toolbar_menu ) TkUtil.bind_context_menu(menuButton, self.toolbar_menu) TkUtil.Tooltip.Tooltip(menuButton, text="Edit Toolbar Menu") self.undoButton = ttk.Button( self.editToolbar, text=UNDO, image=self.toolbarImages[UNDO], command=self.editor.edit_undo ) TkUtil.Tooltip.Tooltip(self.undoButton, text=UNDO) self.redoButton = ttk.Button( self.editToolbar, text=REDO, image=self.toolbarImages[REDO], command=self.editor.edit_redo ) TkUtil.Tooltip.Tooltip(self.redoButton, text=REDO) self.copyButton = ttk.Button( self.editToolbar, text=COPY, image=self.toolbarImages[COPY], command=self.editor.text.event_generate("<<Copy>>"), ) TkUtil.Tooltip.Tooltip(self.copyButton, text=COPY) self.cutButton = ttk.Button( self.editToolbar, text=CUT, image=self.toolbarImages[CUT], command=self.editor.text.event_generate("<<Cut>>"), ) TkUtil.Tooltip.Tooltip(self.cutButton, text=CUT) self.pasteButton = ttk.Button( self.editToolbar, text=PASTE, image=self.toolbarImages[PASTE], command=self.editor.text.event_generate("<<Paste>>"), ) TkUtil.Tooltip.Tooltip(self.pasteButton, text=PASTE) self.findButton = ttk.Button(self.editToolbar, text=FIND, image=self.toolbarImages[FIND], command=self.find) TkUtil.Tooltip.Tooltip(self.findButton, text=FIND) TkUtil.add_toolbar_buttons( self.editToolbar, ( menuButton, self.undoButton, self.redoButton, None, self.copyButton, self.cutButton, self.pasteButton, None, self.findButton, ), ) self.toolbars.append(self.editToolbar)
def close(self, event=None): settings = TkUtil.Settings.Data settings.put(GENERAL, RESTORE, self.restore) if self.restore: geometry = TkUtil.geometry_for_str(self.master.geometry()) position = TkUtil.str_for_geometry(x=geometry.x, y= geometry.y) settings.put(GENERAL, POSITION, position) TkUtil.Settings.save() if self.worker is not None and self.worker.is_alive(): self.state.value = TERMINATING self.update_ui() self.worker.join() # Wait for worker to finish self.quit()
def create_bindings(self): modifier = TkUtil.key_modifier() self.master.bind("<{}-f>".format(modifier), self.find) self.master.bind("<{}-n>".format(modifier), self.new) self.master.bind("<{}-o>".format(modifier), self.open) self.master.bind("<{}-q>".format(modifier), self.close) self.master.bind("<{}-s>".format(modifier), self.save) self.master.bind("<F1>", self.help) for key in "fnoqs": # Avoid conflicts self.unbind_class("Text", "<{}-{}>".format(modifier, key)) # Ctrl+C etc. from the edit menu are already supported by the # Text (Editor) widget. TkUtil.bind_context_menu(self.editor.text, self.context_menu)
def create_window_menu(self): modifier = TkUtil.menu_modifier() self.windowMenu = tk.Menu(self.menubar, name="window") self.windowToolbarMenu = tk.Menu(self.windowMenu) self.windowMenu.add_cascade(label="Toolbars", underline=0, menu=self.windowToolbarMenu) self.windowMenu.add_cascade(label="Dock Windows", underline=0) self.menubar.add_cascade(label="Window", underline=0, menu=self.windowMenu)
def create_widgets(self): self.findLabel = TkUtil.Label(self, text="Find:", underline=1) self.findEntry = ttk.Entry(self, width=25) self.replaceLabel = TkUtil.Label(self, text="Replace:", underline=1) self.replaceEntry = ttk.Entry(self, width=25) self.caseSensitiveCheckbutton = TkUtil.Checkbutton(self, text="Case Sensitive", underline=5, variable=self.caseSensitive) self.wholeWordsCheckbutton = TkUtil.Checkbutton(self, text="Whole Words", underline=0, variable=self.wholeWords) self.findButton = TkUtil.Button(self, text="Find", underline=0, command=self.find, default=tk.ACTIVE, state=tk.DISABLED) self.replaceButton = TkUtil.Button(self, text="Replace", underline=0, command=self.replace, state=tk.DISABLED) self.closeButton = TkUtil.Button(self, text="Close", underline=0, command=self.close) if TkUtil.x11(): self.extendButton = TkUtil.ToggleButton(self, text="Extend", underline=1, command=self.toggle_extend) else: self.extendButton = ttk.Button(self, text="Extend", underline=1, command=self.toggle_extend, image=self.images[UNEXTEND], compound=tk.LEFT) self.extensionWidgets = (self.replaceLabel, self.replaceEntry, self.replaceButton)
def create_view_menu(self): modifier = TkUtil.menu_modifier() viewMenu = tk.Menu(self.menubar) viewMenu.add_checkbutton(label=BOLD, underline=0, image=self.menuImages[BOLD], compound=tk.LEFT, variable=self.bold, command=lambda: self.toggle_button(self.boldButton)) viewMenu.add_checkbutton(label=ITALIC, underline=0, image=self.menuImages[ITALIC], compound=tk.LEFT, variable=self.italic, command=lambda: self.toggle_button(self.italicButton)) viewMenu.add_separator() viewMenu.add_radiobutton(label=ALIGN_LEFT, underline=6, image=self.menuImages[ALIGNLEFT], compound=tk.LEFT, variable=self.alignment, value=tk.LEFT, command=self.toggle_alignment) viewMenu.add_radiobutton(label=ALIGN_CENTER, underline=6, image=self.menuImages[ALIGNCENTER], compound=tk.LEFT, variable=self.alignment, value=tk.CENTER, command=self.toggle_alignment) viewMenu.add_radiobutton(label=ALIGN_RIGHT, underline=6, image=self.menuImages[ALIGNRIGHT], compound=tk.LEFT, variable=self.alignment, value=tk.RIGHT, command=self.toggle_alignment) self.menubar.add_cascade(label="View", underline=0, menu=viewMenu)
def body(self, master): self.notebook = ttk.Notebook(master) self.notebook.enable_traversal() self.generalFrame = ttk.Frame(self.notebook) self.create_general_widgets(self.generalFrame) self.layout_general_widgets(self.generalFrame) self.advancedFrame = ttk.Frame(self.notebook) self.create_advanced_widgets(self.advancedFrame) self.layout_advanced_widgets(self.advancedFrame) self.notebook.add(self.generalFrame, text=GENERAL, underline=-1 if TkUtil.mac() else 0, padding=PAD) self.notebook.add(self.advancedFrame, text=ADVANCED, underline=-1 if TkUtil.mac() else 0, padding=PAD) self.notebook.pack(fill=tk.BOTH) self.create_bindings() return self.notebook
def close(self, event=None): settings = TkUtil.Settings.Data settings.put(BOARD, COLUMNS, self.board.columns) settings.put(BOARD, ROWS, self.board.rows) settings.put(BOARD, MAXCOLORS, self.board.maxColors) settings.put(BOARD, DELAY, self.board.delay) settings.put(BOARD, HIGHSCORE, self.board.highScore) settings.put(GENERAL, SHAPENAME, self.shapeName.get()) settings.put(GENERAL, ZOOM, int(self.zoom.get())) settings.put(GENERAL, SHOWTOOLBAR, bool(self.showToolbar.get())) settings.put(GENERAL, RESTORE, self.restore) if self.restore: geometry = TkUtil.geometry_for_str(self.master.geometry()) position = TkUtil.str_for_geometry(x=geometry.x, y= geometry.y) settings.put(GENERAL, POSITION, position) TkUtil.Settings.save() self.quit()
def create_file_menu(self): # Ctrl is nicer than Control for menus modifier = TkUtil.menu_modifier() fileMenu = tk.Menu(self.menubar, name="apple") fileMenu.add_command(label=NEW, underline=0, command=self.board.new_game, compound=tk.LEFT, image=self.images[NEW], accelerator=modifier + "+N") if TkUtil.mac(): self.master.createcommand("exit", self.close) else: fileMenu.add_separator() fileMenu.add_command(label="Quit", underline=0, command=self.close, compound=tk.LEFT, image=self.images[CLOSE], accelerator=modifier + "+Q") self.menubar.add_cascade(label="File", underline=0, menu=fileMenu)
def create_widgets(self): swatch = TkUtil.swatch(self.foreground, outline="#FFF") self.__swatches[self.foreground] = swatch self.foregroundButton = ttk.Button(self, text="Foreground ", image=swatch, compound=tk.RIGHT, command=lambda: self.__set_color(FOREGROUND)) swatch = self.__get_swatch(self.background) self.backgroundButton = ttk.Button(self, text="Background ", image=swatch, compound=tk.RIGHT, command=lambda: self.__set_color(BACKGROUND))
def create_bindings(self): if not TkUtil.mac(): self.bind("<Alt-b>", lambda *args: self.blinkCheckbutton.invoke()) self.bind("<Alt-f>", lambda *args: self.restoreFontCheckbutton.invoke()) self.bind("<Alt-s>", lambda *args: self.restoreSessionCheckbutton.invoke()) self.bind("<Alt-w>", lambda *args: self.restoreWindowCheckbutton.invoke())
def create_ui(self): self.helpLabel = ttk.Label(self, text=_TEXT, background="white") self.closeButton = TkUtil.Button(self, text="Close", underline=0) self.helpLabel.pack(anchor=tk.N, expand=True, fill=tk.BOTH, padx=PAD, pady=PAD) self.closeButton.pack(anchor=tk.S) self.protocol("WM_DELETE_WINDOW", self.close) if not TkUtil.mac(): self.bind("<Alt-c>", self.close) self.bind("<Escape>", self.close) self.bind("<Expose>", self.reposition)
def set_drag_cursor(self, on=True): if on: cursor = "sizing" # Mac OS X can only use built-in Tk cursors path = os.path.realpath(os.path.join(os.path.dirname(__file__), "images")) if TkUtil.windows(): cwd = None try: cwd = os.getcwd() os.chdir(path) # Paths don't work on Windows 7 self.master.config(cursor="@drag.cur") finally: if cwd is not None: os.chdir(cwd) elif not TkUtil.mac(): # Mask made by http://www.kirsle.net/wizards/xbmask.cgi cursor = ("@" + os.path.join(path, "drag.xbm"), os.path.join(path, "drag_mask.xbm"), "#DF00FF", "white") self.master.config(cursor=cursor) else: self.master.config(cursor="") self.master.update()
def create_toolbar(self): self.toolbar = ttk.Frame(self.master) newButton = ttk.Button(self.toolbar, text=NEW, takefocus=False, image=self.images[NEW], command=self.board.new_game) TkUtil.Tooltip.Tooltip(newButton, text="New Game") zoomLabel = ttk.Label(self.toolbar, text="Zoom:") self.zoomSpinbox = Spinbox(self.toolbar, textvariable=self.zoom, from_=Board.MIN_ZOOM, to=Board.MAX_ZOOM, increment=Board.ZOOM_INC, width=3, justify=tk.RIGHT, validate="all") self.zoomSpinbox.config(validatecommand=( self.zoomSpinbox.register(self.validate_int), "%P")) TkUtil.Tooltip.Tooltip(self.zoomSpinbox, text="Zoom level (%)") self.shapeCombobox = ttk.Combobox(self.toolbar, width=8, textvariable=self.shapeName, state="readonly", values=sorted(Shapes.ShapeForName.keys())) TkUtil.Tooltip.Tooltip(self.shapeCombobox, text="Tile Shape") TkUtil.add_toolbar_buttons(self.toolbar, (newButton, None, zoomLabel, self.zoomSpinbox, self.shapeCombobox)) self.toolbar.pack(side=tk.TOP, fill=tk.X, before=self.board)
def show(self): self.leave() self.tip = tk.Toplevel(self.master) self.tip.withdraw() # Don't show until we have the geometry self.tip.wm_overrideredirect(True) # No window decorations etc. if TkUtil.mac(): self.tip.tk.call("::tk::unsupported::MacWindowStyle", "style", self.tip._w, "help", "none") label = ttk.Label(self.tip, text=self.text, padding=1, background=self.background, wraplength=480, relief=None if TkUtil.mac() else tk.GROOVE, font=tkfont.nametofont("TkTooltipFont")) label.pack() x, y = self.position() self.tip.wm_geometry("+{}+{}".format(x, y)) self.tip.deiconify() if self.master.winfo_viewable(): self.tip.transient(self.master) self.tip.update_idletasks() self.timerId = self.master.after(self.showTime, self.hide)
def update_ui(self, *args): guiState = self.state.value if guiState == WORKING: text = "Cancel" underline = 0 if not TkUtil.mac() else -1 state = "!" + tk.DISABLED elif guiState in {CANCELED, TERMINATING}: text = "Canceling..." underline = -1 state = tk.DISABLED elif guiState == IDLE: text = "Scale" underline = 1 if not TkUtil.mac() else -1 state = ("!" + tk.DISABLED if self.sourceText.get() and self.targetText.get() else tk.DISABLED) self.scaleButton.state((state,)) self.scaleButton.config(text=text, underline=underline) state = tk.DISABLED if guiState != IDLE else "!" + tk.DISABLED for widget in (self.sourceEntry, self.sourceButton, self.targetEntry, self.targetButton): widget.state((state,)) self.master.update() # Make sure the GUI refreshes
def create_file_menu(self): modifier = TkUtil.menu_modifier() fileMenu = tk.Menu(self.menubar, name="apple") fileMenu.add_command(label=NEW, underline=0, command=self.board.new_game, compound=tk.LEFT, image=self.images[NEW], accelerator=modifier + "+N") if TkUtil.mac(): self.master.createcommand("exit", self.close) self.master.createcommand("::tk::mac::ShowPreferences", self.preferences) else: fileMenu.add_separator() fileMenu.add_command(label=PREFERENCES + ELLIPSIS, underline=0, command=self.preferences, image=self.images[PREFERENCES], compound=tk.LEFT) fileMenu.add_separator() fileMenu.add_command(label="Quit", underline=0, command=self.close, compound=tk.LEFT, image=self.images[CLOSE], accelerator=modifier + "+Q") self.menubar.add_cascade(label="File", underline=0, menu=fileMenu)
def create_variables(self): self.caseSensitive = tk.IntVar() self.caseSensitive.set(0) self.wholeWords = tk.IntVar() self.wholeWords.set(0) self.extensionWidgets = () if not TkUtil.x11(): self.images = {} imagePath = os.path.join(os.path.dirname( os.path.realpath(__file__)), "images") for name in (EXTEND, UNEXTEND): filename = os.path.join(imagePath, name + "_16x16.gif") if os.path.exists(filename): self.images[name] = tk.PhotoImage(file=filename)
def create_help_menu(self): helpMenu = tk.Menu(self.menubar, name="help") if TkUtil.mac(): self.master.createcommand("tkAboutDialog", self.about) self.master.createcommand("::tk::mac::ShowHelp", self.help) else: helpMenu.add_command(label=HELP, underline=0, command=self.help, image=self.images[HELP], compound=tk.LEFT, accelerator="F1") helpMenu.add_command(label=ABOUT, underline=0, command=self.about, image=self.images[ABOUT], compound=tk.LEFT) self.menubar.add_cascade(label=HELP, underline=0, menu=helpMenu)
def create_advanced_widgets(self, master): self.delayLabel = TkUtil.Label(master, text="Delay (ms)", underline=0) self.delaySpinbox = Spinbox(master, textvariable=self.delay, from_=Board.MIN_DELAY, to=Board.MAX_DELAY, increment=25, width=4, justify=tk.RIGHT, validate="all") self.delaySpinbox.config( validatecommand=(self.delaySpinbox.register(self.validate_int), "delaySpinbox", "%P")) self.zoomLabel = TkUtil.Label(master, text="Zoom (%)", underline=0) self.zoomSpinbox = Spinbox(master, textvariable=self.options.zoom, from_=Board.MIN_ZOOM, to=Board.MAX_ZOOM, increment=Board.ZOOM_INC, width=4, justify=tk.RIGHT, validate="all") self.zoomSpinbox.config( validatecommand=(self.zoomSpinbox.register(self.validate_int), "zoomSpinbox", "%P")) self.showToolbarCheckbutton = TkUtil.Checkbutton( master, text="Show Toolbar", underline=5, variable=self.showToolbar) self.restoreCheckbutton = TkUtil.Checkbutton(master, text="Restore Position", underline=0, variable=self.restore)
def create_widgets(self): self.findLabel = TkUtil.Label(self, text="Find:", underline=1) self.findEntry = ttk.Entry(self, width=25) self.replaceLabel = TkUtil.Label(self, text="Replace:", underline=1) self.replaceEntry = ttk.Entry(self, width=25) self.caseSensitiveCheckbutton = TkUtil.Checkbutton( self, text="Case Sensitive", underline=5, variable=self.caseSensitive) self.wholeWordsCheckbutton = TkUtil.Checkbutton( self, text="Whole Words", underline=0, variable=self.wholeWords) self.findButton = TkUtil.Button(self, text="Find", underline=0, command=self.find, default=tk.ACTIVE, state=tk.DISABLED) self.replaceButton = TkUtil.Button(self, text="Replace", underline=0, command=self.replace, state=tk.DISABLED) self.closeButton = TkUtil.Button(self, text="Close", underline=0, command=self.close) if TkUtil.x11(): self.extendButton = TkUtil.ToggleButton(self, text="Extend", underline=1, command=self.toggle_extend) else: self.extendButton = ttk.Button(self, text="Extend", underline=1, command=self.toggle_extend, image=self.images[UNEXTEND], compound=tk.LEFT) self.extensionWidgets = (self.replaceLabel, self.replaceEntry, self.replaceButton)
def released(self, event): if self.dock is None: return self.set_drag_cursor(False) if self.winfo_containing(event.x_root, event.y_root) is None: # Dropped outside the window self.dock.undock(event.x_root, event.y_root) else: # Dropped inside the window geometry = TkUtil.geometry_for_str(self.master.winfo_geometry()) middle = geometry.x + (geometry.width // 2) if event.x_root < middle: self.dock.dock_left() else: self.dock.dock_right() self.x = self.y = self.dock = None
def create_help_menu(self): helpMenu = tk.Menu(self.menubar, name="help") if TkUtil.mac(): self.master.createcommand("tkAboutDialog", self.about) self.master.createcommand("::tk::mac::ShowHelp", self.help) else: helpMenu.add_command(label=HELP, underline=0, command=self.help, image=self.menuImages[HELP], compound=tk.LEFT, accelerator="F1") helpMenu.add_command(label=ABOUT, underline=0, command=self.about, image=self.menuImages[ABOUT], compound=tk.LEFT) self.menubar.add_cascade(label=HELP, underline=0, menu=helpMenu)
def on_modified(self, event=None): if not hasattr(self, "modifiedLabel"): self.editor.edit_modified(False) return if self.editor.edit_modified(): text, mac, state = "MOD", True, tk.NORMAL else: text, mac, state = "", False, tk.DISABLED self.modifiedLabel.config(text=text) if TkUtil.mac(): self.master.attributes("-modified", mac) self.fileMenu.entryconfigure(SAVE, state=state) self.fileMenu.entryconfigure(SAVE_AS + ELLIPSIS, state=state) self.saveButton.config(state=state) self.editMenu.entryconfigure(UNDO, state=state) self.undoButton.config(state=state)
def create_widgets(self): swatch = TkUtil.swatch(self.foreground, outline="#FFF") self.__swatches[self.foreground] = swatch self.foregroundButton = ttk.Button( self, text="Foreground ", image=swatch, compound=tk.RIGHT, command=lambda: self.__set_color(FOREGROUND)) swatch = self.__get_swatch(self.background) self.backgroundButton = ttk.Button( self, text="Background ", image=swatch, compound=tk.RIGHT, command=lambda: self.__set_color(BACKGROUND))
def create_bindings(self): self.protocol("WM_DELETE_WINDOW", self.close) if not TkUtil.mac(): self.bind("<Alt-c>", self.close) self.bind("<Alt-e>", lambda *args: self.replaceEntry.focus()) self.bind("<Alt-f>", self.find) self.bind("<Alt-i>", lambda *args: self.findEntry.focus()) self.bind("<Alt-r>", self.replace) self.bind("<Alt-s>", lambda *args: self.caseSensitiveCheckbutton.invoke()) self.bind("<Alt-w>", lambda *args: self.wholeWordsCheckbutton.invoke()) self.bind("<Alt-x>", lambda *args: self.extendButton.invoke()) self.bind("<Return>", self.find) self.bind("<Escape>", self.close) self.findEntry.bind("<KeyRelease>", self.update_ui) self.replaceEntry.bind("<KeyRelease>", self.update_ui)
def add_button(self, master, text, underline, command, default=False, shortcut=None): button = TkUtil.Button(master, text=text, underline=underline, command=command) if default: button.config(default=tk.ACTIVE) button.pack(side=tk.LEFT, padx=PAD, pady=PAD) if shortcut is not None and int(button.cget("underline")) != -1: self.bind(shortcut, command) return button
def create_view_toolbar(self): settings = TkUtil.Settings.Data self.viewToolbar = ttk.Frame(self.toolbarFrame, relief=tk.RAISED) self.viewToolbar.text = FORMAT_TOOLBAR self.viewToolbar.underline = 1 menuButton = ttk.Button(self.viewToolbar, text="Format Toolbar Menu", image=self.toolbarImages[TOOLBARMENU], command=self.toolbar_menu) TkUtil.bind_context_menu(menuButton, self.toolbar_menu) TkUtil.Tooltip.Tooltip(menuButton, text="Format Toolbar Menu") self.fontFamilyCombobox = ttk.Combobox(self.viewToolbar, width=15, textvariable=self.fontFamily, state="readonly", values=TkUtil.font_families()) self.fontFamilyCombobox.bind("<<ComboboxSelected>>", self.update_font) TkUtil.set_combobox_item(self.fontFamilyCombobox, self.fontFamily.get()) TkUtil.Tooltip.Tooltip(self.fontFamilyCombobox, text="Font Family") self.fontSizeSpinbox = Spinbox(self.viewToolbar, width=2, textvariable=self.fontPointSize, from_=6, to=72, justify=tk.RIGHT, validate="all") self.fontSizeSpinbox.config( validatecommand=(self.fontSizeSpinbox.register(self.validate_int), "fontSizeSpinbox", "%P")) TkUtil.Tooltip.Tooltip(self.fontSizeSpinbox, text="Font Point Size") self.boldButton = ttk.Button(self.viewToolbar, text=BOLD, image=self.toolbarImages[BOLD]) self.boldButton.config( command=lambda: self.toggle_button(self.boldButton, self.bold)) TkUtil.Tooltip.Tooltip(self.boldButton, text=BOLD) self.italicButton = ttk.Button(self.viewToolbar, text=ITALIC, image=self.toolbarImages[ITALIC]) self.italicButton.config( command=lambda: self.toggle_button(self.italicButton, self.italic)) TkUtil.Tooltip.Tooltip(self.italicButton, text=ITALIC) TkUtil.add_toolbar_buttons( self.viewToolbar, (menuButton, self.fontFamilyCombobox, self.fontSizeSpinbox, self.boldButton, self.italicButton)) self.toolbars.append(self.viewToolbar)
def create_bindings(self): if not TkUtil.mac(): self.master.bind("<Alt-a>", lambda *args: self.targetEntry.focus()) self.master.bind("<Alt-b>", self.about) self.master.bind("<Alt-c>", self.scale_or_cancel) self.master.bind("<Alt-h>", self.help) self.master.bind("<Alt-i>", lambda *args: self.dimensionCombobox.focus()) self.master.bind("<Alt-o>", lambda *args: self.sourceEntry.focus()) self.master.bind("<Alt-q>", self.close) self.master.bind("<Alt-s>", lambda *args: self.choose_folder(SOURCE)) self.master.bind("<Alt-t>", lambda *args: self.choose_folder(TARGET)) self.master.bind("<F1>", self.help) self.sourceEntry.bind("<KeyRelease>", self.update_ui) self.targetEntry.bind("<KeyRelease>", self.update_ui) self.master.bind("<Return>", self.scale_or_cancel)
def populate_text(self): self.text.insert(tk.END, "{}\n".format(APPNAME), ("title", "center")) self.text.insert( tk.END, "Copyright © 2012-13 Qtrac Ltd. " "All rights reserved.\n", ("center", )) self.text.insert(tk.END, "www.qtrac.eu/pipbook.html\n", ("center", "url", "above5")) self.add_lines(""" This program or module is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. It is provided for educational purposes and is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.""") self.text.insert(tk.END, "\n" + TkUtil.about(self.master, APPNAME, VERSION), ("versions", "center", "above3"))
def create_view_menu(self): modifier = TkUtil.menu_modifier() viewMenu = tk.Menu(self.menubar) viewMenu.add_checkbutton( label=BOLD, underline=0, image=self.menuImages[BOLD], compound=tk.LEFT, variable=self.bold, command=lambda: self.toggle_button(self.boldButton)) viewMenu.add_checkbutton( label=ITALIC, underline=0, image=self.menuImages[ITALIC], compound=tk.LEFT, variable=self.italic, command=lambda: self.toggle_button(self.italicButton)) viewMenu.add_separator() viewMenu.add_radiobutton(label=ALIGN_LEFT, underline=6, image=self.menuImages[ALIGNLEFT], compound=tk.LEFT, variable=self.alignment, value=tk.LEFT, command=self.toggle_alignment) viewMenu.add_radiobutton(label=ALIGN_CENTER, underline=6, image=self.menuImages[ALIGNCENTER], compound=tk.LEFT, variable=self.alignment, value=tk.CENTER, command=self.toggle_alignment) viewMenu.add_radiobutton(label=ALIGN_RIGHT, underline=6, image=self.menuImages[ALIGNRIGHT], compound=tk.LEFT, variable=self.alignment, value=tk.RIGHT, command=self.toggle_alignment) self.menubar.add_cascade(label="View", underline=0, menu=viewMenu)
def undock(self, dock, x=None, y=None): """Warning: On Mac OS X 10.5 undocking works imperfectly. Left and right docking work fine though. """ dock.pack_forget() dock.config(relief=tk.FLAT, borderwidth=0) dock.tk.call("wm", "manage", dock) on_close = dock.register(dock.on_close) dock.tk.call("wm", "protocol", dock, "WM_DELETE_WINDOW", on_close) title = dock.title if hasattr(dock, "title") else "Dock" dock.tk.call("wm", "title", dock, title) minsize = dock.minsize if hasattr(dock, "minsize") else (60, 30) dock.tk.call("wm", "minsize", dock, *minsize) dock.tk.call("wm", "resizable", dock, False, False) if TkUtil.windows(): dock.tk.call("wm", "attributes", dock, "-toolwindow", True) if x is not None and y is not None: self.xy_for_dock[dock] = (x, y) x, y = self.xy_for_dock.get(dock, (None, None)) if x is not None and y is not None: dock.tk.call("wm", "geometry", dock, "{:+}{:+}".format(x, y)) self.__remove_area(dock)
def context_menu(self, event): modifier = TkUtil.menu_modifier() menu = tk.Menu(self.master) if self.editor.text.tag_ranges(tk.SEL): menu.add_command(label=COPY, underline=0, command=lambda: self.editor.text.event_generate( "<<Copy>>"), image=self.menuImages[COPY], compound=tk.LEFT, accelerator=modifier + "+C") menu.add_command(label=CUT, underline=2, command=lambda: self.editor.text.event_generate( "<<Cut>>"), image=self.menuImages[CUT], compound=tk.LEFT, accelerator=modifier + "+X") menu.add_command(label=PASTE, underline=0, command=lambda: self.editor.text.event_generate( "<<Paste>>"), image=self.menuImages[PASTE], compound=tk.LEFT, accelerator=modifier + "+V") menu.add_separator() menu.add_checkbutton(label=BOLD, underline=0, image=self.menuImages[BOLD], compound=tk.LEFT, variable=self.bold, command=lambda: self.toggle_button(self.boldButton)) menu.add_checkbutton(label=ITALIC, underline=0, image=self.menuImages[ITALIC], compound=tk.LEFT, variable=self.italic, command=lambda: self.toggle_button(self.italicButton)) menu.add_separator() menu.add_radiobutton(label=ALIGN_LEFT, underline=6, image=self.menuImages[ALIGNLEFT], compound=tk.LEFT, variable=self.alignment, value=tk.LEFT, command=self.toggle_alignment) menu.add_radiobutton(label=ALIGN_CENTER, underline=6, image=self.menuImages[ALIGNCENTER], compound=tk.LEFT, variable=self.alignment, value=tk.CENTER, command=self.toggle_alignment) menu.add_radiobutton(label=ALIGN_RIGHT, underline=6, image=self.menuImages[ALIGNRIGHT], compound=tk.LEFT, variable=self.alignment, value=tk.RIGHT, command=self.toggle_alignment) menu.tk_popup(event.x_root, event.y_root)
def create_edit_menu(self): editMenu = tk.Menu(self.menubar) shapeMenu = tk.Menu(editMenu) editMenu.add_cascade(label=SHAPE, underline=0, menu=shapeMenu, image=self.images[SHAPE], compound=tk.LEFT) for name in sorted(Shapes.ShapeForName.keys()): shape = Shapes.ShapeForName[name] shapeMenu.add_radiobutton(label=shape.name, underline=shape.underline, value=shape.name, variable=self.shapeName, compound=tk.LEFT, image=self.images[shape.name]) if TkUtil.mac(): self.master.createcommand("::tk::mac::ShowPreferences", self.preferences) else: editMenu.add_command(label=PREFERENCES + ELLIPSIS, underline=0, command=self.preferences, image=self.images[PREFERENCES], compound=tk.LEFT) editMenu.add_checkbutton(label="Show Toolbar", underline=5, onvalue=True, offvalue=False, variable=self.showToolbar, command=self.toggle_toolbar) self.menubar.add_cascade(label="Edit", underline=0, menu=editMenu)
def create_bindings(self): # Can't use Ctrl for key bindings modifier = TkUtil.key_modifier() self.master.bind("<{}-n>".format(modifier), self.board.new_game) self.master.bind("<{}-q>".format(modifier), self.close) self.master.bind("<F1>", self.help)
def validate_int(self, number): return TkUtil.validate_spinbox_int(self.zoomSpinbox, number)
def __validate_int(self, spinbox, number): spinbox = getattr(self, spinbox) return TkUtil.validate_spinbox_int(spinbox, number)
def force_update_toolbars(self, *args): TkUtil.layout_in_rows(self.toolbarFrame, self.master.winfo_width(), DEFAULT_TOOLBAR_HEIGHT, self.toolbars, True)
def __get_swatch(self, color): swatch = self.__swatches.get(color) if swatch is None: swatch = TkUtil.swatch(color) self.__swatches[color] = swatch return swatch
def validate_int(self, spinboxName, number): return TkUtil.validate_spinbox_int(getattr(self, spinboxName), number)
def validate(self, number): return TkUtil.validate_spinbox_float(self.amount_spinbox, number)
def create_bindings(self): if not TkUtil.mac(): for letter in "dlmrstz": self.bind("<Alt-{}>".format(letter), self.handle_shortcut)
def create_widgets(self): self.sourceLabel = ttk.Label(self, text="Source Folder:", underline=-1 if TkUtil.mac() else 1) self.sourceEntry = ttk.Entry(self, width=30, textvariable=self.sourceText) self.sourceButton = TkUtil.Button( self, text="Source...", underline=0, command=lambda *args: self.choose_folder(SOURCE)) self.helpButton = TkUtil.Button(self, text="Help", underline=0, command=self.help) self.targetLabel = ttk.Label(self, text="Target Folder:", underline=-1 if TkUtil.mac() else 1) self.targetEntry = ttk.Entry(self, width=30, textvariable=self.targetText) self.targetButton = TkUtil.Button( self, text="Target...", underline=0, command=lambda *args: self.choose_folder(TARGET)) self.aboutButton = TkUtil.Button(self, text="About", underline=1, command=self.about) self.statusLabel = ttk.Label(self, textvariable=self.statusText) self.scaleButton = TkUtil.Button(self, text="Scale", underline=1, command=self.scale_or_cancel, default=tk.ACTIVE, state=tk.DISABLED) self.quitButton = TkUtil.Button(self, text="Quit", underline=0, command=self.close) self.dimensionLabel = ttk.Label(self, text="Max. Dimension:", underline=-1 if TkUtil.mac() else 6) self.dimensionCombobox = ttk.Combobox( self, textvariable=self.dimensionText, state="readonly", values=("50", "100", "150", "200", "250", "300", "350", "400", "450", "500")) TkUtil.set_combobox_item(self.dimensionCombobox, "400")
def update_toolbars(self, event=None): TkUtil.layout_in_rows(self.toolbarFrame, self.master.winfo_width(), DEFAULT_TOOLBAR_HEIGHT, self.toolbars)
def validate(self, number=None): return TkUtil.validate_spinbox_float(self.spinbox, number)
def create_bindings(self): if not TkUtil.mac(): self.bind("<Alt-l>", lambda *args: self.columnsSpinbox.focus()) self.bind("<Alt-r>", lambda *args: self.rowsSpinbox.focus()) self.bind("<Alt-m>", lambda *args: self.maxColorsSpinbox.focus())
def create_bindings(self): if not TkUtil.mac(): self.bind("<Alt-p>", lambda *args: self.passwordEntry.focus()) self.bind("<Alt-u>", lambda *args: self.usernameEntry.focus()) self.usernameEntry.bind("<KeyRelease>", self.update_ui) self.passwordEntry.bind("<KeyRelease>", self.update_ui)