4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PythonとTkinterを使ってプログラミング

Last updated at Posted at 2012-02-12

#encoding:utf8

from Tkinter import *
import tkFileDialog as dialog

def save(root, text):
data = text.get('0.0', END)
filename = dialog.asksaveasfilename(
parent = root,
filetypes=[('Text', '*.txt')],
title='名前を付けて保存...')
writer = open(filename, 'w')
writer.write(data.encode('utf8'))
writer.close()

def quit(root):
root.destroy()

window = Tk()
text = Text(window)
text.pack()

menubar = Menu(window)
filemenu = Menu(menubar)
filemenu.add_command(label='保存', command=lambda : save(window, text))
filemenu.add_command(label='終了', command=lambda : quit(window))

menubar.add_cascade(label='ファイル', menu=filemenu)
window.config(menu=menubar)

window.mainloop()

'''
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in call
return self.func(*args)
File "menu.py", line 23, in
filemenu.add_command(label='Save', command=lambda : save(window, text))
File "menu.py", line 11, in save
writer.write(data)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

'''

'''
初めてのコンピュータサイエンス gui/menu.pyよりそのままだとエラーだったので
writer.write(data)ではエラーが出たので
writer,write(data.encode('utf8')と、data.encode('utf8')を追加してみた。
'''

4
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?