コードを整理/定義する最良の方法を学ぼうとしています。以下を例に取ると、tkMessageBox コマンドを 2 回記述する必要があります。getText() 内で定義を作成して参照しようとしましたが、うまくいきませんでした。
質問がありますのでよろしくお願いします
1) tkMessageBox コマンドを def などに配置して getText() 内でも参照できるようにコードを配置するにはどうすればよいですか?
2) ベスト プラクティスを考慮して、このコードを別の方法でレイアウトする必要がありますか? もしそうなら、どのように/なぜですか?
前もって感謝します
import Tkinter as tk
import tkMessageBox
import base64
myText = 'empty'
def getText():
global myText
myText = inputBox.get()
entered = "You entered: " + myText
encoded = "We encoded: " + base64.encodestring(myText)
Button1 = tk.Button(root, command = tkMessageBox.showinfo("Press me", entered))
Button1.pack()
Button2 = tk.Button(root, command = tkMessageBox.showinfo("Press me", encoded))
Button2.pack()
root.destroy()
root = tk.Tk()
# Text label
simpleTitle = tk.Label(root)
simpleTitle['text'] = 'Please enter your input here'
simpleTitle.pack()
# The entry box widget
inputBox = tk.Entry(root)
inputBox.pack()
# The button widget
button = tk.Button(root, text='Submit', command=getText)
button.pack()
tk.mainloop()