第1步:創(chuàng)建一個(gè)窗口
要?jiǎng)?chuàng)建文本編輯器,我們需要一個(gè)窗口。輸入文件:
import sys
v=sys.python_version if “2.7” in v:
from Tkinter import *
elif “3.3” in v or “3.4” in v:
from tkinter import *
root=Tk(“Text Editor”) root.mainloop()
然后如果你在Mac上去Cmd + Shift - 搜索并打開(kāi)“終端” - 然后輸入
python /pathto/texteditor.py
然后按回車。
如果您在Windows上,搜索并打開(kāi)命令提示符,請(qǐng)鍵入
python /pathto/texteditor.py
并按Enter鍵。你應(yīng)該看一個(gè)如上圖所示的屏幕。
恭喜!
第2步:添加文本小部件
現(xiàn)在我們需要添加一些內(nèi)容鍵入。
在root = Tk()下面添加兩行,如下所示:
text=Text(root) text.grid()
然后像第一步一樣再次運(yùn)行該文件。當(dāng)你點(diǎn)擊它時(shí),你應(yīng)該有一個(gè)更大的屏幕,里面有一個(gè)文本框。它將如上圖所示。
您的完整代碼現(xiàn)在將如下所示:
import sys
v=sys.version() if “2.7” in v: from Tkinter import * elif “3.3” in v or “3.4” in v: from tkinter import * root=Tk(“Text Editor”) text=Text(root) text.grid() root.mainloop()
步驟3:保存文本
您需要能夠保存文本,因此我們將添加一個(gè)按鈕進(jìn)行保存。
在導(dǎo)入Tkinter下添加
import tkFileDialog
并在導(dǎo)入tkinter下添加
import tkinter.tkFileDialog
在您添加的最后一行下寫下:
def saveas():
global text t = text.get(“1.0”, “end-1c”) savelocation=tkFileDialog.asksaveasfilename() file1=open(savelocation, “w+”) file1.write(t) file1.close() button=Button(root, text=“Save”, command=saveas) button.grid()
單擊按鈕將保存文件。
完整代碼:
import sys
v=sys.version() if “2.7” in v: 《立方厘米= 1》 from Tkinter import *
import tkFileDialog elif “3.3” in v or “3.4” in v: from tkinter import *
import tkinter.tkFileDialog root=Tk(“Text Editor”)
text=Text(root) text.grid()
步驟4:字體轉(zhuǎn)換器
在您添加的最后一行下,添加:
def saveas(): global text t = text.get(“1.0”, “end-1c”) savelocation=tkFileDialog.asksaveasfilename() file1=open(savelocation, “w+”) file1.write(t)
恭喜!您已完成一個(gè)非常簡(jiǎn)單的文本編輯器。如果您想要更多這樣的教程,請(qǐng)告訴我這里的內(nèi)容。
運(yùn)行該文件以使用它!
完整代碼:
file1.close() button=Button(root, text=“Save”, command=saveas)
責(zé)任編輯:wv
-
文本編輯器
+關(guān)注
關(guān)注
0文章
28瀏覽量
8108 -
python
+關(guān)注
關(guān)注
56文章
4797瀏覽量
84689
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
圖紙編輯器的基本操作
![圖紙<b class='flag-5'>編輯器</b>的基本操作](https://file1.elecfans.com//web1/M00/F4/D3/wKgZoWcy17KAduugAAJUjR6RFrE742.png)
【北京迅為】itop-龍芯2k1000開(kāi)發(fā)指南Linux基礎(chǔ)入門vim 編輯器
![【北京迅為】itop-龍芯2k1000開(kāi)發(fā)指南Linux基礎(chǔ)入門vim <b class='flag-5'>編輯器</b>](https://file1.elecfans.com/web1/M00/F3/B0/wKgaoWcbQP2AAoylAACi1I_-VVA641.png)
Vivado編輯器亂碼問(wèn)題
![Vivado<b class='flag-5'>編輯器</b>亂碼問(wèn)題](https://file1.elecfans.com/web1/M00/F2/F1/wKgZoWcONPGAGIULAAME7s40W_I762.jpg)
vim編輯器命令模式使用方法
vim編輯器如何使用
嵌入式學(xué)習(xí)-Vim編輯器之Vim常用操作命令
Vim編輯器之Vim常用操作命令
嵌入式學(xué)習(xí)-常用編輯器之Vim編輯器
常用編輯器之Vim編輯器
致敬經(jīng)典:在國(guó)產(chǎn)開(kāi)源操作系統(tǒng) RT-Thread 重溫 UNIX 彩色終端
![致敬經(jīng)典:在國(guó)產(chǎn)開(kāi)源操作系統(tǒng) RT-Thread 重溫 UNIX 彩色終端](https://file1.elecfans.com/web2/M00/C4/8A/wKgZomX0EhWACv8DAAAUet8ikhs451.png)
TSMaster 中 Hex 文件編輯器使用詳細(xì)教程
![TSMaster 中 Hex 文件<b class='flag-5'>編輯器</b>使用詳細(xì)教程](https://file.elecfans.com/web2/M00/40/07/pYYBAGJrUk2AaMaTAAAQONQtdzo461.jpg)
評(píng)論