Ruby 入門指南 - 完成版的 EncryptGUI





































Ruby 入門指南 - 完成版的 EncryptGUI






我們的 EncryptGUI 還剩兩個按鈕 ClearCopy 還沒實際建置功能




完整程式請參考

  • encrypt.rb

  • encryptgui.rb

  • guidemo.rb





Clear 的目的就是用來清除使用者輸入與結果,程式如下
# 清除所有輸入、輸出
def clearMethod
@e = nil
@userinput = ""
@result = ""
@inputField.value = ""
@outputField.value = ""

@displayText.text = "It's done."
end



這裡除了將 @userinput@result@inputField.value@outputField.value 都設定為空字串 (string) 外, @e 也重新設定為 nil
@e = nil



因此使用者點擊 Clear 之後,所有設定都會歸零,如果要編碼或解碼文字必須重新輸入文字,也都得 NewLoad Encrypt 物件 (object) ,才能順利 EncodeDecode


清除工作完畢,訊息欄顯示 "It's done." 。


Copy 則是把編碼結果拷貝到系統剪貼簿,當然,如果使用者沒有按過 EncodeDecode@result 就會是空字串,這樣拷貝是沒有意義的,因此這裡先做個條件判斷
# 拷貝編碼結果到剪貼簿
def copyMethod
if @result == ""
@displayText.text = "Copy denied!!"
else
TkClipboard.set(@result)
@displayText.text = "It is already copied to the clipboard."
end
end



如果不是空字串就會進行拷貝到剪貼簿的動作,這裡是用 TkClipboard 類別 (class) 的 set 方法 (method) ,然後直接以 @result 當參數 (parameter)
TkClipboard.set(@result)



來執行看看囉!下面是 Clear



下面是 Copy



拷貝過後就可以貼到其他地方



好了,我們已經學完開發 EncryptGUI 了,下一步是?


























中英文術語對照
字串string
物件object
類別class
方法method
參數parameter



您可以繼續參考
GUI 篇

  • GUI 的基本概念

  • 加入 command

  • 整合 Encrypt 類別

  • GUI 中的編碼與解碼

  • 存檔與載入

  • 完成版的 EncryptGUI

  • 下一步





相關目錄

回 Ruby 入門指南
回 Ruby 教材
回首頁



參考資料

http://www.ruby-doc.org/docs/ProgrammingRuby/html/ext_tk.html
http://www.tutorialspoint.com/ruby/ruby_tk_guide.htm







沒有留言:




















window.___gcfg = { 'lang': 'zh-TW' };





Popular posts from this blog

Mái uốn xoăn như “bà thím” vẫn được loạt sao Hàn yêu thích, nhờ sự thay đổi này mà ai cũng trẻ trung hơn hẳn

Cô bé "The Voice Kids Anh" nhận ngay nút vàng tại "Got Talent Mỹ" với màn trình diễn quá đáng yêu

qBittorrent 4.1.1 免安裝中文版 - 取代uTorrent的BT下載器