Simple GUI Notepad Using Ruby

GUI Notepad Using Ruby Code require 'tk' class Notepad def saveFile file = File.open("note", "w") ...

Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Thursday, April 13, 2017

Simple GUI Notepad Using Ruby

GUI Notepad Using Ruby


Code

require 'tk'

class Notepad
  def saveFile
    file = File.open("note", "w")
    thetext = @entry.get("1.0", 'end')
    file.write(thetext)  
    file.close
    @text.value = "Saved"
  end

  def initialize
    ph = { 'padx' => 60, 'pady' => 20 }     # common options
    p = proc {saveFile}
    @text = TkVariable.new
    root = TkRoot.new { title "Notepad" }
    top = TkFrame.new(root)
    TkLabel.new(top) {text    'Enter Text:' ; pack(ph) }
    @entry = TkText.new(top) do
      width 40
      height 10
      borderwidth 1
      font TkFont.new('times 12')
      pack("padx"=> "5", "pady"=> "1")
    end
    # @entry.pack(ph)
    TkButton.new(top) {text 'Save'; command p; pack ph}
    TkButton.new(top) {text 'Exit'; command {proc exit}; pack ph}
    top.pack('fill'=>'both', 'side' =>'top')
    @entry.insert('1.0', File.read("note"))
  end
end

Notepad.new
Tk.mainloop

Output