Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf928dc6

Browse files
committed
add text editor using tkinter tutorial 2
1 parentd815002 commitf928dc6

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

‎gui-programming/text-editor/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#[How to Make a Text Editor using Tkinter in Python](https://www.thepythoncode.com/article/text-editor-using-tkinter-python)
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Import
2+
fromtkinterimport*
3+
fromtkinterimportscrolledtext
4+
fromtkinterimportfiledialog
5+
importctypes
6+
importsys
7+
8+
# Increas Dots Per inch so it looks sharper
9+
ctypes.windll.shcore.SetProcessDpiAwareness(True)
10+
11+
# Setup Variables
12+
13+
appName='Simple Text Editor'
14+
nofileOpenedString='New File'
15+
16+
currentFilePath=nofileOpenedString
17+
18+
# Viable File Types, when opening and saving files.
19+
fileTypes= [("Text Files","*.txt"), ("Markdown","*.md")]
20+
21+
# Tkinter Setup
22+
window=Tk()
23+
24+
# Set the first column to occupy 100% of the width
25+
window.grid_columnconfigure(0,weight=1)
26+
27+
window.title(appName+" - "+currentFilePath)
28+
29+
# Window Dimensions in Pixel
30+
window.geometry('500x400')
31+
32+
# Handler Functions
33+
deffileDropDownHandeler(action):
34+
globalcurrentFilePath
35+
36+
# Opening a File
37+
ifaction=="open":
38+
file=filedialog.askopenfilename(filetypes=fileTypes)
39+
40+
window.title(appName+" - "+file)
41+
42+
currentFilePath=file
43+
44+
withopen(file,'r')asf:
45+
txt.delete(1.0,END)
46+
txt.insert(INSERT,f.read())
47+
48+
# Making a new File
49+
elifaction=="new":
50+
currentFilePath=nofileOpenedString
51+
txt.delete(1.0,END)
52+
window.title(appName+" - "+currentFilePath)
53+
54+
# Saving a file
55+
elifaction=="save"oraction=="saveAs":
56+
ifcurrentFilePath==nofileOpenedStringoraction=='saveAs':
57+
currentFilePath=filedialog.asksaveasfilename(filetypes=fileTypes)
58+
59+
withopen(currentFilePath,'w')asf:
60+
f.write(txt.get('1.0','end'))
61+
62+
window.title(appName+" - "+currentFilePath)
63+
64+
deftextchange(event):
65+
window.title(appName+" - *"+currentFilePath)
66+
67+
# Widgets
68+
69+
# Text Area
70+
txt=scrolledtext.ScrolledText(window,height=999)
71+
txt.grid(row=1,sticky=N+S+E+W)
72+
73+
# Bind event in the widget to a function
74+
txt.bind('<KeyPress>',textchange)
75+
76+
# Menu
77+
menu=Menu(window)
78+
79+
# set tearoff to 0
80+
fileDropdown=Menu(menu,tearoff=False)
81+
82+
# Add Commands and and their callbacks
83+
fileDropdown.add_command(label='New',command=lambda:fileDropDownHandeler("new"))
84+
fileDropdown.add_command(label='Open',command=lambda:fileDropDownHandeler("open"))
85+
86+
# Adding a seperator between button types.
87+
fileDropdown.add_separator()
88+
fileDropdown.add_command(label='Save',command=lambda:fileDropDownHandeler("save"))
89+
fileDropdown.add_command(label='Save as',command=lambda:fileDropDownHandeler("saveAs"))
90+
91+
menu.add_cascade(label='File',menu=fileDropdown)
92+
93+
# Set Menu to be Main Menu
94+
window.config(menu=menu)
95+
96+
# Enabling "open with" by looking if the second argument was passed.
97+
iflen(sys.argv)==2:
98+
currentFilePath=sys.argv[1]
99+
100+
window.title(appName+" - "+currentFilePath)
101+
102+
withopen(currentFilePath,'r')asf:
103+
txt.delete(1.0,END)
104+
txt.insert(INSERT,f.read())
105+
106+
# Main Loop
107+
window.mainloop()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp