import tkinter import os from tkinter インポート * #投稿のために上記の場所を取得するには から tkinter.messagebox import * #使用するダイアログを開くには必要に応じて from tkinter.filedialog import * < / p> 注。 メッセージボックスはメモ帳という名前の白いボックスにメッセージを書き込むために使用され、ダイアログファイルは表示/em>ダイアログボックスに使用されます。システムのどこからでもファイルを開くか、ファイルを特定の位置または場所に保存します。 メニューを追加します: b> #コントロールを追加(ウィジェット) self .__ thisTextArea.grid(sticky = N + E + S + W) #新しいファイルを開く self .__ thisFileMenu.add_command(label = <コードクラス= "string"> "New" 、 コマンド = self .__ newFile) #既存のファイルを開くには self .__ thisFileMenu.add_command(label = "Open" 、 コマンド = self .__ openFile) #現在のファイルを保存 self .__ thisFileMenu.add_command(label = "保存" 、 コマンド = self .__ saveFile) #ダイアログに行を作成します self .__ thisFileMenu.add_separator() #停止するには self .__ thisFileMenu.add_command(label = "Exit" 、 コマンド = self .__ quitApplication) self .__ thisMenuBar.add_cascade(label = "ファイル" 、 menu = self .__ thisFileMenu) #ストロークを追加するには self .__ thisEditMenu.add_command(label = "Cut" 、 コマンド = self .__ cut) #コピー機能を与えるには self .__ thisEditMenu.add_command(label = "コピー" 、 コマンド = self .__ copy) #挿入関数を追加するには self .__ thisEditMenu.add_command(label = "貼り付け" 、 コマンド = self .__ Paste) #編集を有効にするには self .__ thisMenuBar.add_cascade(label = "編集" 、 メニュー = self c ode> .__ thisEditMenu) #メモ帳の説明関数を作成するには self .__ thisHelpMenu.add_command(label = "メモ帳について" 、 コマンド = self .__ showAbout) self .__ thisMenuBar.add_cascade(label = "ヘルプ" 、 メニュー = self .__ thisHelpMenu) < br> 自己 .__ root.config(menu = self .__ thisMenuBar) self .__ thisScrollBar.pack(side = 右、塗りつぶし = Y) #スクロールバーは自動的に調整されます #コンテンツに応じて self .__ thisScrollBar.config(コマンド = self .__ thisTextArea .yview) self .__ thisTextArea.config(yscrollcommand = sel f .__thisScrollBar。 set ) td> tr> tbody> table> このコードを使用して、メモ帳ウィンドウにメニューを追加し、コピー、貼り付け、保存などを追加します。 機能の追加: b> def __ quitApplication( self ): self .__ root.destroy() #exit() def __ showAbout( self ): showinfo( "メモ帳" 、 "Mrinal Verma" ) < br> def __ openFile( self ): self .__ file = askopenfilename(defaultextension = "。 txt " 、 ファイルタイプ = [( "すべてのファイル" 、 "*。*" )、 ( "テキストドキュメント" 、 "*。txt " )]) if self .__ f ile = = "": #開くファイルがありません self .__ file = なし else : #ファイルを開いてみてください #ウィンドウタイトルを設定します self .__ root.title(os.path。ベース名( self .__ file) + コード><コードclass ="string">"-テパッドなし" ) self .__ thisTextArea.delete( 1.0 、END) ファイル = open ( self .__ file、 "r" ) self .__ thisTextArea.insert( 1.0 、 ファイル 。 read()) file 。 close() def __ newFile( self ): self .__ root.title( "無題-メモ帳" ) self .__ file = なし self .__ thisTextArea.delete( 1.0 、END) def __ saveFile( self c ode> ): if self .__ファイル = = なし : #新しいファイルとして保存 self .__ file = asksaveasfilename(initialfile = ’Untitled.txt’ 、 defaultextension = "。 txt " 、 ファイルタイプ = [( "すべてのファイル" 、 "*。*" )、 ( "テキストドキュメント" 、 "*。txt " )]) if self .__ file = = "": self .__ファイル = なし else : #ファイルの保存を試みます ファイル = open ( self .__ file、 "w" ) f ile 。書き込み( self .__ thisTextArea.get( 1.0 、END)) ファイル 。 close() #ウィンドウのタイトルを変更する self .__ root.title( os.path.basename ( self .__ file) + "-メモ帳" ) else : ファイル = open ( self .__ file、 "w" ) ファイル 。 write( self .__ thisTextArea.get( 1.0 、END)) ファイル 。 close() def __ cut( self ): self .__ thisTextArea.event_generate( "&lt;&lt; Cut&gt;&gt;" ) def __ copy( self ): self .__ thisTextArea.event_generate( "&lt;&lt; Copy&gt;&gt;" ) def __ Paste( self ): self .__ thisTextArea.event_generate( "&lt; &lt;貼り付け&gt; &gt; " ) td> tr> tbody> table> これにより、メモ帳に必要なすべての機能が追加されました。フォントサイズ、フォントの色、太字、下線などの他の機能を追加することもできます。 後のメインコードすべてを組み合わせる: b> import tkinter import os from tkinter import * from tkinter.messagebox import * from tkinter.filedialog import * class メモ帳: __ root = Tk() #デフォルトのウィンドウの幅と高さ __ thisWidth = 300 __ thisHeight = 300 < / p> __ thisTextArea = テキスト(__root) < / p> __ thisMenuBar = メニュー(__ルート) __ thisFileMenu = メニュー(__ thisMenuBar、ティアオフ = 0 ) __ thisEditMenu = Menu(__ thisMenuBar、tearoff = 0 ) __ thisHelpMenu = Menu(__ thisMenuBar、tearoff = 0 ) #スクロールバーを追加 __ thisScrollBar = スクロールバー(__ thisTextArea) __ file = なし def __ init __( self 、 * * kwargs): #アイコンを設定 try : self .__ root .wm_iconbitmap( "Notepad.ico" ) を除く: pass #ウィンドウサイズを設定します(デフォルトは300x300) try : self .__ thisWidth = kwargs [ ’width’ ] KeyError: pass < br> 試してください : self .__ thisHeight = kwargs [ ’height’ ] KeyErrorを除く: pass #ウィンドウテキストを設定 self .__ root.title( "無題-メモ帳" ) #ウィンドウを中央に配置 screenWidth = self .__root。 winfo_screenwidth() screenHeight = self .__ root .winfo_screenheight() #左側の場合 左側 = (screenWidth / 2 ) - ( self .__ thisWidth / 2 ) #正しく top = (screenHeight / 2 ) - ( self 。 __thisHeight / 2 ) #上下の場合 self .__ root.geometry( ’%dx%d +%d +%d’ % ( self .__ thisWidth、 self .__ thisHeight、 左、上)) #テキスト領域のサイズを自動的に変更するには / code> pass #ウィンドウテキストを設定 self .__ root.title( "無題-メモ帳" ) #中央のウィンドウ screenWidth = self .__ root.winfo_screenwidth() screenHeight = self .__ root.winfo_screenheight() #左側の場合 left = (screenWidth / 2 )
Shop
Learn programming in R: courses $
Best Python online courses for 2022 $
Best laptop for Fortnite $
Best laptop for Excel $
Best laptop for Solidworks $
Best laptop for Roblox $
Best computer for crypto mining $
Best laptop for Sims 4 $
Latest questions
NUMPYNUMPY
psycopg2: insert multiple rows with one query
12 answers
NUMPYNUMPY
How to convert Nonetype to int or string?
12 answers
NUMPYNUMPY
How to specify multiple return types using type-hints
12 answers
NUMPYNUMPY
Javascript Error: IPython is not defined in JupyterLab
12 answers
Wiki
Python OpenCV | cv2.putText () method
numpy.arctan2 () in Python
Python | os.path.realpath () method
Python OpenCV | cv2.circle () method
Python OpenCV cv2.cvtColor () method
Python - Move item to the end of the list
time.perf_counter () function in Python
Check if one list is a subset of another in Python
Python os.path.join () method
|