Will you use Microsoft Word? When I write a university report, when I submit it in word format, I start Word and write the report. Recently, I've been writing reports using Google Docs more and more ... but I don't write enough sentences to open Word in the world ... I don't even have to launch a browser and open Google Docs ... Isn't it just a memo? (Just me?) At that time, you often want to count the number of characters ... (Wata ry) That's why I made a character counter in Python. For busy people, the source code is [here. ](# 4-Source code)
Mac OS Catalina Python 3.7.6 PySimpleGUI 4.18.2
When it comes to implementing a GUI in Python, I think the first thing that comes to mind is Tkinter. I thought about implementing it in Tkinter, but somehow I thought it was a little complicated ... so I searched for another good library and found it, ** [PySimpleGUI](https: /) /pysimplegui.readthedocs.io/en/latest/) **. So, this time we will proceed with the implementation using this library.
As a function that I personally need for the character counter,
--Area for entering the number of characters
--Count button
--Quit button
It's pretty good if there is enough. The minimum required functions are sufficient. Because I just move it a little. We will implement these three.
First, install PySimple GUI. If you haven't installed it yet, in the terminal
pip install pysimplegui
Enter.
Once the installation is complete, we will start implementing it immediately.
What I imported this time
re
PySimpleGUI as sg
There are two.
Set Theme at the beginning. I chose `DarkAmber`
because I like how it looks. This area is your choice. For more information, go to here.
wordcounter.py
import PySimpleGUI as sg
import re
sg.theme('DarmAmber')
This completes the Theme settings. Next, consider the GUI layout. The layout is
layout = []
It can be set freely in the form of an array like. I
wordcounter.py
import PySimpleGUI as sg
import re
sg.theme('DarkAmber')
layout = [
[sg.Text('Word Counter')],
[sg.Multiline((), size=(70, 30))],
[sg.Submit(button_text='Count', size=(5, 1)), sg.Button('Quit', size=(5, 1))],
[sg.Button('Delete', size=(5, 1)), sg.Button('Save', size=(5, 1))]
]
I did it like this. Then it looks like this.
I think it's pretty intuitive to write.
Roughly speaking,
-- Text ()` `` displays the text area on one line --`` `Multiline ()` `` displays a multi-line text area --`` `Submit ()` `` will display the button --
Button ()
displays the button
size=()
Allows you to specify the size of the element. 1=It can be counted with one character, so in my casemultiline(), size=(70, 30)
Then, the size of the text area is70 characters vertically x 30 characters horizontallyIt is set like this.
To be honest, I'm not sure about the difference between `Submit ()`
and `Button ()`
, but this time it works fine so I'll leave it as it is ...
When the layout is complete, the rest sets the movement when the button is pressed. And before that, let's display the created layout.
wordcounter.py
import PySimpleGUI as sg
import re
sg.theme('DarkAmber')
layout = [
[sg.Text('Word Counter')],
[sg.Multiline((), size=(70, 30))],
[sg.Submit(button_text='Count', size=(5, 1)), sg.Button('Quit', size=(5, 1))],
[sg.Button('Delete', size=(5, 1)), sg.Button('Save', size=(5, 1))]
]
window = sg.Window('Word Counter', layout)
#abridgement
window.close()
I added
window = sg.Window('Word Counter', layout)
When
window.close()
is. The rest of the program is written between the two.
while
I will implement it with a statement.
wordcounter.py
#abridgement
while True:
event, values = window.read()
if event is None:
print('No words')
break
if event == 'Count':
show_message = len(values[0]) - 1
print(show_message)
sg.popup('Result', show_message)
if event == 'Quit':
break
if event == 'Delete':
sg.PopupYesNo('Are you really?')
if 'Yes':
values[0] = re.sub()
Below, if
and the following are described individually.
if event is None:
print('No words')
break
Describes the movement when the x button is pressed.
if event == 'Count':
show_message = len(values[0]) - 1
print(show_message)
sg.popup('Result', show_message)
This is the main. Assign the value of the text area to the variable show_message
as counted by
len ()``. In PySimpleGUI, it seems that the values put in the text box or area can be obtained by ``
values []` ``. There seems to be another way.
len(values[0]) - 1
The reason for doing this is that after verifying it several times, for some reason, one more number than the number of characters entered in the text is output, so-The book tail is adjusted with 1. why? Anyway, I'm glad that it works because it works. If you know a better way to write, please let us know.
if event == 'Quit':
break
As it is. Press `` `Quit``` to exit the program.
if event == 'Delete':
sg.PopupYesNo('Are you really?')
if 'Yes':
values[0] = re.sub()
I haven't implemented this yet, but I want to delete all the strings in the text area when I press `` `Delete``` ... but it doesn't work. I think I'll use a regular expression to erase it, but I think there's a better way ... but I can't think of it and leave it here. Well, it's okay if you don't have this feature, but I'd like to make it even more convenient ... Please help anyone who knows it.
This completes the tool with the features I wanted.
Below is the source code.
wordcounter.py
import PySimpleGUI as sg
import re
sg.theme('DarkAmber')
layout = [
[sg.Text('Word Counter')],
[sg.Multiline((), size=(70, 30))],
[sg.Submit(button_text='Count', size=(5, 1)), sg.Button('Quit', size=(5, 1))],
[sg.Button('Delete', size=(5, 1)), sg.Button('Save', size=(5, 1))]
]
window = sg.Window('Word Counter', layout)
while True:
event, values = window.read()
if event is None:
print('No words')
break
if event == 'Count':
show_message = len(values[0]) - 1
print(show_message)
sg.popup('Result', show_message)
if event == 'Quit':
break
if event == 'Delete':
sg.PopupYesNo('Are you really?')
if 'Yes':
values[0] = re.sub()
window.close()
I was able to create a character counter in this way. There are many similar tools without having to write code, but it would be interesting to have one fun to create your own tool. I'm quite attached to it and use it relatively (laughs) It doesn't support save function or output, it's just a substitute to copy and paste a character string and use it only to judge the number of characters now. I think it will be more convenient if various other functions are added. Thank you for reading this far.
If you use Tkinter, try using PySimpleGUI I touched the Python GUI library "PySimpleGUI" PySimpleGUI PySimpleGUI Cookbook Introducing My Boom PySimple GUI Basic usage of PySimple GUI
Recommended Posts