I. Writing ahead
What do you think of when you think of programmers? Some people think that programmers symbolize high salary, some people think that programmers are all dead fat nerds, and some people think of 996 and ICU.
Programmers in the eyes of others: flying keyboard, cool switching screen, all kinds of unintelligible character code.
But what about programmers in reality? For many programmers, there is no problem that cannot be solved by Baidu and Google, and there is no function that cannot be realized by ctrl + c and ctrl + v.
So as a programmer, how do you make yourself look more "professional"? The answer is to speed up their typing speed, knocking the code may be wrong, but this 13 is necessary to install!
However, there are still a lot of people who don't type that fast and may need to be trained, but since you are a programmer, why not write your own training typing gadget out?
II. Basic ideas
PyQT5 is used to develop a GUI that displays a sentence and lets the user type it, then compares the input with the given sentence, records the time spent, and finally outputs the correct rate. In order to keep typing, a "next sentence" function is also needed. After the realization of these two basic functions, a simple typing training gadget has been made.
III. Interface design
1. Environment Configuration
PyQT5 development environment configuration has been described in the previous blog, so I will not repeat here, if you are not clear, you can point to thehere areView.
2. Interface design
To design the interface, you have to open QtDesigner, then create a new project and select Widget:
Then drag the control inside on it, which is still very convenient, the main controls include Label, Text Edit, Push Button, etc., double-click the control can be modified on the right side of the control of the various properties. The final design of the interface is as follows:
3. Generate Python code
After designing the interface, you save the result and get a file with a .ui extension. This file cannot be used directly, but needs to be converted to Python code. You need to use the pyuic5 command, if you don't know how to do that, you can click thehere areCheck out my last blog.
IV. Slot functions
1. Introduction to slot functions
To do function design, you need to know about slot functions. Slots are ordinary C++ member functions. Slots are a very important concept in Qt development software, what connects to signals in Qt are slots, which we generally call slot functions.
There are four parameters when using signals:
1) sender: the object that sends the signal;
2) signal: the signal sent by the sending object;
3) receiver: the object that receives the signal;
(4) slot: the receiving object in the signal received after the need to call the function (slot function).
2. Usage
(1) First method
The first step is to click on "Edit Signals/Slots" in QtDesigner, then left click on the button and drag the mouse to another Label:
Step 2: Select the specific method and function in the pop-up settings box.
This is simple but flawed, the problem is that you can't customize the method, you can only use the given method.
(2) Second method
Find "Signal/Slot Editor" in the bottom right corner and click on it, then you can click "+" to create a signal.
This approach allows us to customize it, but it's not exactly convenient, especially if you don't know exactly what method to implement.
(3) Third method
With the connect() method, the passed parameter is a method name. Example:
(func)
V. Functional realization
1. Inherit function
Using pyuic5 to generate py file defines a Ui_Form class, which contains the definition of various controls, etc. If we want to add other features and modify them directly in this py file, then it will not be inconvenient to update the interface afterwards, so the best way is to inherit the Ui_Form class, and then add modifications to the inherited class. The reference code is as follows:
import sys from PyQt5 import QtWidgets from import Ui_Form class MyForm(Ui_Form, ): def __init__(self): super(MyForm, self).__init__() (self) if __name__ == '__main__': app = () my_form = MyForm() my_form.show() (app.exec_())
2. Hide the display controls
Sometimes we may need to hide some controls, here is a brief introduction to several methods.
1) setHidden(bool) Set whether the control is hidden or not;
2) hide() Hide the control;
These two methods of hiding will hide the control completely, i.e., they do not preserve the position occupied by the control. However, if you want to preserve the position, you can use this method below:
def click(self): """ Called when the button is clicked :return. """ self.get_time() the_input = () # Calculation accuracy count = 0 for i in range(len(the_input)): if the_input[i] == [i]: count += 1 accuracy = count / len() * 100 # print(accuracy) self.show_label() # Setting up alert messages info = "Kind of a shame you got it right: %.2f%% " % accuracy if accuracy != 100 else "Congratulations on getting it all right! Keep up the good work!" self.info_lable.setText(info)
3. Judge the input content
This function is realized by getting the content of the input box when the "Submit" button is clicked, comparing it with the given text, and finally returning the result to the display.
For lazy people like me, clicking buttons for this kind of operation are laborious, so it is better to add a shortcut key, which is much more convenient, you can use the following method to set up:
# Setting shortcuts self.submit_btn.setShortcut('ctrl+e')
VI. Operational results
A screenshot of the final run is shown below:
You can also click on the next sentence after submitting to continue the training:
The full code has been uploaded toGitHub!
summarize
The above is a small introduction to the use of Python to create a typing training gadgets, I hope to help you, if you have any questions welcome to leave me a message, I will reply to you in a timely manner!