開発環境
- 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 5.の解答を求めてみる。
コード
#!/usr/bin/env python3
import tkinter
class F2C:
def __init__(self, parent):
self.parent = parent
# Model
self.fahrenheit = tkinter.DoubleVar()
self.celsius = tkinter.DoubleVar()
# View
self.label = tkinter.Label(
self.parent, text='Temperature in Fahrenheit')
self.label.pack()
self.entry = tkinter.Entry(self.parent, textvariable=self.fahrenheit)
self.entry.pack()
self.label_celsius = tkinter.Label(
self.parent, textvariable=self.celsius)
self.label_celsius.pack()
self.convert = tkinter.Button(self.parent,
text='convert',
command=self.f2c)
self.convert.pack()
self.quit = tkinter.Button(
self.parent, text='quit', command=self.parent.destroy)
self.quit.pack()
# Controller
def f2c(self):
self.celsius.set((self.fahrenheit.get() - 32) * 5 / 9)
if __name__ == "__main__":
app = tkinter.Tk()
counter = F2C(app)
app.mainloop()
入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))
% ./sample5.py
%
0 コメント:
コメントを投稿