Skip to content

Commit a57b42b

Browse files
committed
Text Widget demo - tkinter
1 parent 28cdc2b commit a57b42b

File tree

2 files changed

+154
-1
lines changed

2 files changed

+154
-1
lines changed

src/program13.py

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
'''
2+
Created on Jan 14, 2018
3+
4+
@author: aditya
5+
6+
Tkinter Text Widget demo
7+
Text widget is complex and very useful. It can have multiple lines
8+
and various kind of manipulations on text are possible
9+
Uses: Data logging, user instructions, message display, text input from user in survey like forms.
10+
11+
Please refer tk documentation for text widget details for more information
12+
13+
Indexing in text widget:
14+
- Lines are numbered starting with '1'.
15+
- Characters are numbered starting with '0'.
16+
example - Base string, '1.0', refers to the first character in the first line of text box.
17+
- 'end' refers to the position after the last character of a line
18+
- Please refer the indices section of tkinter documentation of Text Widget for more details
19+
20+
'''
21+
22+
import tkinter as tk
23+
from tkinter import ttk
24+
25+
26+
class App(object):
27+
28+
def __init__(self, master):
29+
self.master = master
30+
self.text = tk.Text(self.master, width = 50, height = 20)
31+
self.text.pack()
32+
self.text.config(wrap = 'word') # wrap = 'word' or 'none' or 'char' - default is 'char'
33+
34+
self.btn0 = ttk.Button(self.master, text ='Insert random Text in TExt field', command=self.randomTextEntry)
35+
self.btn0.pack()
36+
37+
self.btn1 = ttk.Button(self.master, text = 'Get all text in line 1', command = self.getline1text)
38+
self.btn1.pack()
39+
40+
self.btn2 = ttk.Button(self.master, text = 'Get All Text', command = self.getAllText)
41+
self.btn2.pack()
42+
43+
self.btn3 = ttk.Button(self.master, text = 'Insert "My Name is \nAdi" on 3rd Line', command = self.putTextat3rdLine)
44+
self.btn3.pack()
45+
46+
self.btn4 = ttk.Button(self.master, text = 'Delete First Character of Text', command = self.deleteFirst)
47+
self.btn4.pack()
48+
49+
self.btn5 = ttk.Button(self.master, text = 'Delete from 5 to 7 of line 3', command = self.delete5to8on3)
50+
self.btn5.pack()
51+
52+
self.btn6 = ttk.Button(self.master, text = 'Delete first 3 lines', command = self.deleteFirst3Lines)
53+
self.btn6.pack()
54+
55+
self.btn7 = ttk.Button(self.master, text = 'Replace line 1 with random text', command = self.replaceRandom)
56+
self.btn7.pack()
57+
58+
self.btn8 = ttk.Button(self.master, text = 'Disable Text Field', command = self.disableEnable)
59+
self.btn8.pack()
60+
61+
62+
self.lbl1 = ttk.Label(self.master, text = 'Waiting to display TEXT....')
63+
self.lbl1.pack()
64+
65+
def randomTextEntry(self):
66+
text = '''Random Text
67+
It real sent your at.
68+
Amounted all shy set why followed declared.
69+
Repeated of endeavor mr position kindness offering ignorant so up.
70+
Simplicity are melancholy preference considered saw companions.
71+
Disposal on outweigh do speedily in on. Him ham although thoughts entirely drawings.
72+
Acceptance unreserved old admiration projection nay yet him.
73+
Lasted am so before on esteem vanity oh.
74+
75+
Moments its musical age explain.
76+
But extremity sex now education concluded earnestly her continual.
77+
Oh furniture acuteness suspected continual ye something frankness.
78+
Add properly laughter sociable admitted desirous one has few stanhill.
79+
Opinion regular in perhaps another enjoyed no engaged he at.
80+
It conveying he continual ye suspected as necessary.
81+
Separate met packages shy for kindness.
82+
83+
For norland produce age wishing.
84+
To figure on it spring season up.
85+
Her provision acuteness had excellent two why intention.
86+
As called mr needed praise at.
87+
Assistance imprudence yet sentiments unpleasant expression met surrounded not.
88+
Be at talked ye though secure nearer.
89+
90+
So by colonel hearted ferrars.
91+
Draw from upon here gone add one.
92+
He in sportsman household otherwise it perceived instantly.
93+
Is inquiry no he several excited am.
94+
Called though excuse length ye needed it he having.
95+
Whatever throwing we on resolved entrance together graceful.
96+
Mrs assured add private married removed believe did she.
97+
98+
Prepared is me marianne pleasure likewise debating.
99+
Wonder an unable except better stairs do ye admire.
100+
His and eat secure sex called esteem praise.
101+
So moreover as speedily differed branched ignorant.
102+
Tall are her knew poor now does then.
103+
Procured to contempt oh he raptures amounted occasion.
104+
One boy assure income spirit lovers set.
105+
'''
106+
self.text.insert('1.0', text)
107+
108+
def disableEnable(self):
109+
if self.btn8['text']=='Disable Text Field':
110+
self.btn8.config(text='Enable Text Field')
111+
self.text.config(state = 'disabled')
112+
else:
113+
self.btn8.config(text='Disable Text Field')
114+
self.text.config(state = 'normal')
115+
116+
def replaceRandom(self):
117+
self.text.replace('1.0', '1.0 lineend', 'This is random line 1.')
118+
119+
def deleteFirst3Lines(self):
120+
self.text.delete('1.0', '3.0 lineend + 1 chars')
121+
122+
def delete5to8on3(self):
123+
self.text.delete('3.5', '3.8')
124+
125+
def deleteFirst(self):
126+
self.text.delete('1.0')
127+
128+
def putTextat3rdLine(self):
129+
self.text.insert('1.0 + 2 lines lineend', '. My Name is \nAdi. And Yo \nAnd Yo...')
130+
131+
def getline1text(self):
132+
if self.btn1['text']== 'Get all text in line 1':
133+
self.lbl1.config(text = self.text.get('1.0', '1.end'))
134+
self.btn1.config(text = 'Refresh')
135+
else:
136+
self.lbl1.config(text = 'Waiting to display TEXT....')
137+
self.btn1.config(text = 'Get all text in line 1')
138+
139+
def getAllText(self):
140+
if self.btn2['text']=='Get All Text':
141+
self.lbl1.config(text = self.text.get('1.0', 'end'))
142+
self.btn2.config(text = 'Refresh')
143+
else:
144+
self.lbl1.config(text = 'Waiting to display TEXT....')
145+
self.btn2.config(text = 'Get All Text')
146+
147+
def launchApp():
148+
root = tk.Tk()
149+
App(root)
150+
tk.mainloop()
151+
152+
if __name__=='__main__':
153+
launchApp()

src/program7.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, master):
2323
self.spinbxyear = tk.Spinbox(master, from_ = 1900,
2424
to = datetime.datetime.now().year,
2525
textvariable = self.year)
26-
self.spinbxyear.pack()
26+
self.spinbxyear.pack()
2727

2828
#####################################################################
2929

0 commit comments

Comments
 (0)