您的系統上必須已安裝 Python 3 和 Tkinter 才能創建此筆記本。您可以根據您的系統要求下載相應的 Python 包。成功安裝 Python 後,需要安裝 Tkinter(Python 的 GUI 包)。
使用此命令安裝 Tkinter:
pip install python-tk
Tkinter 導入:
注意。 Message Box 用於在名為 Notepad 的白框中寫入消息,Dialog File 用於顯示/em> 對話框時從系統上的任何位置打開文件或將文件保存到特定位置或位置。 添加菜單:
|
使用此代碼,我們將向記事本窗口添加菜單,並添加複制、粘貼、保存等內容。
添加功能:
def
__ quitApplication (
self
):
self
.__ root.destroy ()
#exit()
def
__ showAbout (
self
):
showinfo(
"記事本"
,
"Mrinal Verma"
)
__ openFile (
self
):
self
.__ file
=
askopenfilename (defaultextension
=
". txt"
,
文件類型
=
[(
"所有文件"
,
" *. * "
),
(
"文本文檔"
,
"*.txt"
)])
if
self
.__ file
=
=
"":
# 沒有要打開的文件
self
.__ file
=
無
else
:
# 嘗試打開文件
# 設置窗口標題
self
.__ root.title (os.path. basename (
self
.__ 文件)
+ 代碼> <代碼class = "string"> " - 沒有 tepad "
)
self
.__ thisTextArea.delete (
1.0
, END)
文件
=
打開
(
self
.__ 文件,
"r"
)
self
.__ thisTextArea.insert (
1.0
,
文件
。讀取 ())
file
。 close ()
def
__ newFile (
self
):
self
.__ root.title(
"Untitled - Notepad"
)
self
.__ file
=
None
self
.__ thisTextArea.delete (
1.0
, END)
def
__ saveFile(
self
):
if
self
.__ 文件
=
=
None
:
# 另存為新文件
self
.__ file
=
asksaveasfilename (initialfile
=
’ Untitled.txt’
,
defaultextension
=
"。 txt"
,
文件類型
=
[(
"所有文件"
,
"*.*"
),
(
"文本文檔"
,
"*.txt"
)])
if
self
.__ file
=
=
"":
self
.__ 文件
=
None
else
:
# 嘗試保存文件
file
=
打開
(
self
.__ 文件,
"w"
)
f ile
。寫 (
self
.__ thisTextArea.get (
1.0
, END))
文件
。 close()
#改變窗口的標題
self
.__ root.title (os.path.basename (
self
.__ file)
+
"- 記事本"
)
else
:
文件
=
打開
(代碼>
self
.__file,
"w"
)
文件
。寫 (
self
.__ thisTextArea.get (
1.0
, END))
文件
。 close()
def
__ cut (
self
):
self
.__ thisTextArea.event_generate(
"<<Cut>>"
)
def
__ 複製 (
self
):
self
.__ thisTextArea.event_generate(
"""Copy"""
)
def
__ paste (
self
):
self
.__ thisTextArea.event_generate(
"< <<粘貼 >""
)
這個我們已經添加了記事本所需的所有功能,您還可以添加其他功能,如字體大小、字體顏色、粗體、下劃線等。
後面的主要代碼結合所有:
import
tkinter
import
os
from
tkinter
import
*
from
tkinter.messagebox
import
*
from
tkinter.filedialog
import
*
類
記事本:
__ root
=
tk()
#默認窗口寬高
__ thisWidth
=
300
代碼>
__ thisHeight
=
300
__ thisTextArea
=
文本 (__root)
__ thisMenuBar
=
Menu (__ root)
__ thisFileMenu
=
Menu (__ thisMenuBar, tearoff
=
0
)
__ thisEditMenu
=
Menu (__ thisMenuBar, tearoff
=
0
)
__ thisHelpMenu
=
Menu (__ thisMenuBar, tearoff
=
0
)
# 添加滾動條
__ thisScrollBar
=
滾動條 (__ thisTextArea)
__ file
=
None
def
__ init __ (
self
,
*
*
kwargs ):
# 設置圖標
try
:
self
.__ root .wm_iconbitmap (
"Notepad.ico"
)
except
:
pass
#設置窗口大小(默認300x300)
:
self
.__ thisWidth
=
kwargs [
’width’
]
除
KeyError:
pass
try
:
self
.__ thisHeight
=
kwargs [
’height’
]
除外
KeyError:
code>
pass
#設置窗口文字
self
.__ root.title (
"Untitled - Notepad"
)
# 居中窗口
screenWidth
=
self
.__ root. winfo_screenwidth ()
screenHeight
=
self
.__ root .winfo_screenheight ()
# 為左
left
=
(screenWidth
/
2
)
-
(
self
.__ thisWidth
/
2
)
# Right-ally
top
=
/
2
)
-
(
self
. __thisHeight
/
2
)
# 上下分
self
.__ root.geometry (
’%dx%d +%d +%d’
%
(
self
.__ thisWidth,
self
.__ thisHeight,
left, top))
# 讓文本區域自動調整大小
/code>
pass
# 設置窗口文本
self
.__ root.title (
"Untitled - Notepad"
)
# 中心窗口
screenWidth
=
self
.__ root.winfo_screenwidth ()
screenHeight
=
self
.__ root.winfo_screenheight()
# 為左邊
left
=
(screenWidth
/
2
)