3 Replies - 3067 Views - Last Post: 04 October 2011 - 06:03 AM

#1 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2972
  • View blog
  • Posts: 11,223
  • Joined: 15-July 08

Bare Bones Notepad using Tkinter

Posted 15 August 2009 - 08:40 PM

Description: None, except to have the default installation of Python on your system. Tkinter should be includedThis snippet shows how to make a very basic text editor with Tkinter
#! /usr/bin/python

from Tkinter import *

class Notepad:
    
    def __init__(self):

        # Set up the root widget
        self.root = Tk()

        # Set up the menubar
        menubar = Menu(self.root)

        # File menu, which includes save, load, and quit       
        filemenu = Menu(menubar)
        filemenu.add_command(label="Save", command=self.save)
        filemenu.add_command(label="Load", command=self.load)
        filemenu.add_separator()
        filemenu.add_command(label="Quit", command=self.root.quit)
        menubar.add_cascade(label="File", menu=filemenu)
        self.root.config(menu=menubar)

        # Now add the Text Widget
        self.text = Text(self.root)

        # This line allows it to be resized        
        self.text.pack(expand=YES, fill=BOTH)

        self.root.mainloop()        

    def save(self):
        from tkFileDialog import asksaveasfilename

        # Prompt for file, and then write to it
        fileName = asksaveasfilename()
        try:
            file = open(fileName, 'w')
            textoutput = self.text.get(0.0, END)
            file.write(textoutput)
        except:
            pass
        finally:
            file.close()

    def load(self):
        from tkFileDialog import askopenfilename

        # Prompt for filename then open from it
        fileName = askopenfilename()
        try:
            file = open(fileName, 'r')
            contents = file.read()

            self.text.delete(0.0, END)
            self.text.insert(0.0, contents)
        except:
            pass

# Start the Notepad
Notepad()


Is This A Good Question/Topic? 0
  • +

Replies To: Bare Bones Notepad using Tkinter

#2 dave_6_10   User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 19-March 08

Re: Bare Bones Notepad using Tkinter

Posted 03 November 2009 - 10:53 AM

so what notepad program did you type this code on?
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2972
  • View blog
  • Posts: 11,223
  • Joined: 15-July 08

Re: Bare Bones Notepad using Tkinter

Posted 03 November 2009 - 01:59 PM

I use IDLE and Geany.
Was This Post Helpful? 0
  • +
  • -

#4 ATC3   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 54
  • Joined: 04-October 11

Re: Bare Bones Notepad using Tkinter

Posted 04 October 2011 - 06:03 AM

Nice job
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1