Programming with Python and Tkinter

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 ='Save As ...') 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 ='save', command = lambda: save (window, text)) filemenu.add_command (label ='end', command = lambda: quit (window))

menubar.add_cascade (label ='file', 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)

'''

''' Since it was an error if it was left as it was from the first computer science gui / menu.py Since an error occurred in writer.write (data) I added writer, write (data.encode ('utf8') and data.encode ('utf8'). '''

Recommended Posts

Programming with Python and Tkinter
3. 3. AI programming with Python
Python programming with Atom
Competitive programming with python
Programming with Python Flask
Encryption and decryption with Python
Working with tkinter and mouse
Python and hardware-Using RS232C with Python-
Run Label with tkinter [Python]
python with pyenv and venv
Network programming with Python Scapy
Works with Python and R
Shining life with Python and OpenCV
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
[Python] font family and font with matplotlib
[Introduction to Python3 Day 1] Programming and Python
Scraping with Node, Ruby and Python
Scraping with Python, Selenium and Chromedriver
[Python] Object-oriented programming learned with Pokemon
Scraping with Python and Beautiful Soup
JSON encoding and decoding with python
Hadoop introduction and MapReduce with Python
Easy Python + OpenCV programming with Canopy
[GUI with Python] PyQt5-Drag and drop-
Reading and writing NetCDF with Python
Async / await with Kivy and tkinter
I played with PyQt5 and Python3
Reading and writing CSV with Python
Multiple integrals with Python and Sympy
Coexistence of Python2 and 3 with CircleCI (1.0)
Easy modeling with Blender and Python
[Python] Creating multiple windows with Tkinter
Sugoroku game and addition game with python
FM modulation and demodulation with Python
Compare HTTP GET / POST with cURL (command) and Python (programming)
Solving with Ruby and Python AtCoder ABC178 D Dynamic programming
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
I tried to make GUI tic-tac-toe with Python and Tkinter
Communicate between Elixir and Python with gRPC
Play video with sound with python !! (tkinter / imageio)
Data pipeline construction with Python and Luigi
Calculate and display standard weight with python
FizzBuzz with Python3
Competitive programming with python Local environment settings
Python programming note
Scraping with Python
Monitor Mojo outages with Python and Skype
Statistics with python
FM modulation and demodulation with Python Part 3
[Automation] Manipulate mouse and keyboard with Python
Scraping with Python
Passwordless authentication with RDS and IAM (Python)
Python with Go
Eating and comparing programming languages: Python and Ruby
Python installation and package management with pip
Using Python and MeCab with Azure Databricks
POST variously with Python and receive with Flask