当前位置: 首页>>代码示例>>Python>>正文


Python ScrolledText.tag_add方法代码示例

本文整理汇总了Python中ScrolledText.tag_add方法的典型用法代码示例。如果您正苦于以下问题:Python ScrolledText.tag_add方法的具体用法?Python ScrolledText.tag_add怎么用?Python ScrolledText.tag_add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ScrolledText的用法示例。


在下文中一共展示了ScrolledText.tag_add方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: import ScrolledText [as 别名]
# 或者: from ScrolledText import tag_add [as 别名]

#.........这里部分代码省略.........

    def toggle_theme(self):
        if self.theme == 'light':
            self.editor.config(bg='black', fg='white', insertbackground='white',highlightcolor='black')
            self.editor.frame.config(bg='black')
            # theme for misspelled words
            self.editor.tag_configure("misspelled", foreground="red", underline=True)
            self.theme = 'dark'
        else:
            self.editor.config(bg='white', fg='black', insertbackground='black',highlightcolor='white')
            self.editor.frame.config(bg='white')
            # theme for misspelled words
            self.editor.tag_configure("misspelled", foreground="red", underline=True)
            self.theme = 'light'

    def toggle_wrap(self):
        # self.editor.cget('wrap') # gets the config value
        if not self.options.wrap.get():
            self.editor.config(wrap='none')
        else:
            self.editor.config(wrap='word')

    def find(self):
        find_text = tkSimpleDialog.askstring("Textee", "Enter text to search", initialvalue=self.find_text)
        if find_text:
            if find_text == self.find_text:
                start_pos = self.editor.search(find_text, self.editor.index('insert'), stopindex=END, nocase=True)
            else:
                start_pos = self.editor.search(find_text, '1.0', stopindex=END, nocase=True)
                
            self.find_text = find_text
            if(start_pos):
                end_pos = '%s+%sc' % (start_pos, len(self.find_text))
                self.editor.tag_add(SEL, start_pos, end_pos)
                self.editor.mark_set(INSERT, end_pos) # mark the cursor to end of find text to start editing
                self.editor.see(INSERT) # bing the cursor position in the viewport incase of long text causinng scrollbar
                self.editor.focus_set() # strangely tkinter doesnt return focus after prompt
            else:
                tkMessageBox.showinfo("Textee", "No morw matches found")
        else:
            self.editor.focus_set() # strangely tkinter doesnt return focus after prompt

    def find_and_replace(self):
        #show the custom dialog
        self.find_and_replace_dialog = TexteeFindAndReplaceDialog(self.master)
        if self.find_and_replace_dialog.find_text:
            start_pos = self.editor.search(self.find_and_replace_dialog.find_text, '1.0', stopindex=END, nocase=True)
            # lazy recursive replace for all the matched elements, no need to parse whole dataset again and again
            while start_pos:
                self.editor.delete(start_pos, '%s+%sc' % (start_pos, len(self.find_and_replace_dialog.find_text)))
                self.editor.insert(start_pos, self.find_and_replace_dialog.replace_with)
                # break after first replace if replace all flag is off
                if not self.find_and_replace_dialog.replace_all:
                    break
                start_pos = self.editor.search(self.find_and_replace_dialog.find_text, '%s+%sc' % (start_pos, len(self.find_and_replace_dialog.replace_with)), stopindex=END, nocase=True)


    def select_font(self):
        self.font_dialog = TexteeFontDialog(self.master)
        fs = 12 # default size for any font selected

        if self.font_dialog.selected_font_family:
            ff = self.font_dialog.selected_font_family
        
        if self.font_dialog.selected_font_size:
            fs = self.font_dialog.selected_font_size
开发者ID:rajeshvaya,项目名称:playground-textee,代码行数:70,代码来源:Textee.py

示例2: __init__

# 需要导入模块: import ScrolledText [as 别名]
# 或者: from ScrolledText import tag_add [as 别名]

#.........这里部分代码省略.........
        self.parent.bind_all("<Command-Option-N>", self.new_tab)
        self.file_menu.add_separator()
        self.file_menu.add_command(label="Open", command=self.open_command, accelerator="Cmd+O")
        self.parent.bind_all("<Command-o>", self.open_command)
        self.parent.bind_all("<Command-O>", self.open_command)
        self.file_menu.add_command(label="Save", command=self.save_command, accelerator="Cmd+S")
        self.parent.bind_all("<Command-s>", self.save_command)
        self.parent.bind_all("<Command-S>", self.save_command)
        self.file_menu.add_separator()
        self.file_menu.add_command(label= "Quit", command= self.exit_program, accelerator="Cmd+W")
        self.parent.bind_all("<Command-w>", self.exit_program)
        self.parent.bind_all("<Command-W>", self.exit_program)

        # Edit Menu
        self.edit_menu = tk.Menu(self.menuBar, tearoff=0)
        self.menuBar.add_cascade(label= "Edit", menu= self.edit_menu)
        self.edit_menu.add_command(label = "Cut", command = self.cut_command, accelerator="Cmd+X")
        self.parent.bind_all("<Command-Shift-x>", self.cut_command)
        self.parent.bind_all("<Command-Shift-X>", self.cut_command)
        self.edit_menu.add_command(label = "Copy", command = self.copy_command, accelerator="Cmd+C")
        self.parent.bind_all("<Command-Shift-c>", self.copy_command)
        self.parent.bind_all("<Command-Shift-C>", self.copy_command)
        self.edit_menu.add_command(label = "Paste", command = self.paste_command, accelerator="Cmd+V")
        self.parent.bind_all("<Command-Shift-v>", self.paste_command)
        self.parent.bind_all("<Command-Shift-V>", self.paste_command)
        self.edit_menu.add_separator()
        self.edit_menu.add_command(label= "Find", command= self.find_command)

        parent.config(menu=self.menuBar)
        
##################################################

    def open_command(self, event=None):
        file = tkFileDialog.askopenfile(parent=root,mode='rb',title='Select a file')
        if file != None:
            contents = file.read()
            self.textWidget.insert("1.0",contents)
            file.close()

    def save_command(self, event=None):
        file = tkFileDialog.asksaveasfile(mode= 'w')
        if file != None:
            data = self.textWidget.get("1.0", END+'-1c')   #strip trailing \n at EOF
            file.write(data)
            file.close()

    def exit_program(self, event=None):
        if tkMessageBox.askokcancel("Quit", "Are you sure you want to quit?"):
            self.parent.destroy()
     
    #Opens up new text widget correctly but screws up closing the parent window via the menu       
    def new_command(self, event=None):
        win = Toplevel()
        SimpleTextEditor.__init__(self, win)
        
    #Currently under construction    
    def new_tab(self, event=None):
        #self.parent.add(self.textWidget, text="new tab", state='normal')
        new_frame = tk.Frame(self.parent)
        self.parent.add(new_frame, text='new', state='normal')
        
    def cut_command(self, event=None):
        text = self.textWidget.get(SEL_FIRST, SEL_LAST)
        self.textWidget.delete(SEL_FIRST, SEL_LAST)
        self.textWidget.clipboard_clear()
        self.textWidget.clipboard_append(text) 
        
    def copy_command(self, event=None):
        text = self.textWidget.get(SEL_FIRST, SEL_LAST)
        self.textWidget.clipboard_clear()
        self.textWidget.clipboard_append(text)
        
    def paste_command(self, event=None):
        try:
            text = self.textWidget.selection_get(selection= 'CLIPBOARD')
            self.textWidget.insert(INSERT, text)
        except TclError:
            pass
            
    def find_command(self):                  
        # Currently only works for highlighted text and will only find one match to the string, not all of the instances
        # Also does not work with key binding because of askstring hanging window bug :/
        target = askstring('SimpleEditor', 'Search String?')
        if target:
            where = self.textWidget.search(target, INSERT, END)
            if where:
                print where
                pastit = where + ('+%dc' % len(target))
                self.textWidget.tag_add(SEL, where, pastit)
                self.textWidget.mark_set(INSERT, pastit)
                self.textWidget.see(INSERT)
                self.textWidget.focus()       

    def about_command(self):
        label = tkMessageBox.showinfo("About", "A super duper simple text editor. It's now available through github, thanks to Jason!")                


    #dummy function as a place holder while constructing further functionality        
    def dummy_funct(self, event=None):
        print"The life of a function is often very lonely..."
开发者ID:austenLacy,项目名称:simple-OSX-text-editor,代码行数:104,代码来源:text_editor.py


注:本文中的ScrolledText.tag_add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。