python顯示中文

# -*- coding: utf-8 -*-
#頁首要有utf8的標籤

print u"我是天才"
#中文前面要+u

print "我是天才"
#這樣的話出來就是亂碼

以下為輸入跟輸出的範例


# -*- coding: utf-8 -*-

import Tkinter as tk
win=tk.Tk()

def buttonclick():
    name=entry.get().encode('utf-8')
    result.config(text= name)
#要從輸入框get一個中文輸入, 後面要加上encode('utf-8')

entry=tk.Entry(win)
entry.grid(column=1,row=0)

label=tk.Label(win, text=u"輸入名字:")   #顯示中文前面都要加u
label.grid(column=0,row=0)    

result=tk.Label(win, text=u"名字")   #顯示結果的標籤
result.grid(column=1,row=1)

button=tk.Button(win, text=u"名字叫做",command=buttonclick)
button.grid(column=0,row=1)     #顯示元件


win.mainloop()

留言

這個網誌中的熱門文章

tkinter翻頁實作

python的模組module匯入語法