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


Python ScrolledText.selection_get方法代码示例

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


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

示例1: __init__

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

    def __init__(self, parent):
        self.parent = parent
        self.textWidget = ScrolledText(parent, width=80, height=50, font=(tkFont.Font(family= "Consolas", size= "12")))
        self.textWidget.pack()

        self.menuBar = tk.Menu(parent, tearoff=0)
        # About Menu
        self.about_menu = tk.Menu(self.menuBar, tearoff= 0)
        self.menuBar.add_cascade(label= "Text Perfect", menu= self.about_menu)
        self.about_menu.add_command(label= "About", command= self.about_command)

        # File Menu
        self.file_menu = tk.Menu(self.menuBar, tearoff = 0)
        self.menuBar.add_cascade(label = "File", menu=self.file_menu)
        self.file_menu.add_command(label="New", command=self.new_command, accelerator="Cmd+N")
        self.parent.bind_all("<Command-n>", self.new_command)
        self.parent.bind_all("<Command-N>", self.new_command)
        self.file_menu.add_command(label="New Tab", command=self.new_tab, accelerator="Cmd+Opt+N")
        self.parent.bind_all("<Command-Option-n>", self.new_tab)
        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
            
#.........这里部分代码省略.........
开发者ID:austenLacy,项目名称:simple-OSX-text-editor,代码行数:103,代码来源:text_editor.py


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