開発環境
- macOS Catalina - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Python 3.8 (プログラミング言語)
Practical Programming: An Introduction to Computer Science Using Python 3.6 (Paul Gries(著)、Jennifer Campbell(著)、Jason Montojo(著)、Pragmatic Bookshelf)のChapter 16(Creating Graphical User Interfaces)、Exercise 6.の解答を求めてみる。
コード
#!/usr/bin/env python3
import tkinter
import tkinter.filedialog as dialog
class TextEditor:
def __init__(self, parent):
self.parent = parent
# View
self.frame = tkinter.Frame(self.parent)
self.frame.pack()
self.text = tkinter.Text(self.frame)
self.text.pack()
menubar = tkinter.Menu(self.frame)
filemenu = tkinter.Menu(menubar)
filemenu.add_command(label='保存', command=self.save)
filemenu.add_command(label='終了', command=self.quit)
menubar.add_cascade(label='File', menu=filemenu)
self.parent.config(menu=menubar)
# Controller
def save(self):
data = self.text.get('0.0', tkinter.END)
filename = dialog.asksaveasfilename(
parent=self.frame,
filetypes=[('Text', '*.txt')],
title='Save as...',
)
with open(filename, 'w') as writer:
writer.write(data)
def quit(self):
self.parent.destroy()
if __name__ == "__main__":
window = tkinter.Tk()
counter = TextEditor(window)
window.mainloop()
入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))
% ./sample6.py
% ./sample6.py
% cat sample6.txt
こんにちは。
Hello, World!
%
0 コメント:
コメントを投稿