Python learning journey: in Python made a typing exercise gadget


He said programmer, what would you think of it? Some people think that programmers symbol of high-paying, some people think that fat house programmers are dead, and others think it is 996 and ICU.


Programmers in the eyes of others: fast keystrokes, cool switch the screen, look at all kinds of character codes do not understand.

However, the reality of programmers to it? For many programmers, Baidu and Google does not solve the problem, nor ctrl + c and ctrl + v can not be achieved function.


So as a programmer, how to make them look more “professional” mean? The answer is to speed up their typing speed was knocking on the code may be wrong, but this 13 is to be installed!

However, there are still a lot of people typing is not so fast, you may need to train the next, but since as a programmer, why not write your own gadgets a training typing out of it?


Second, the basic idea

Use PyQT5 develop GUI, and allows users to achieve the display input sentence, and then the contents of the input sentence given for comparison, while recording the time used, the final output will be correct rate. In order to be able to continue typing, a function needs to be done “under a” the. In the realization of these two basic functions, a simple typing training gadgets do come out.


Third, interface design

1. Environment Configuration

PyQT5 development environment configuration on a blog already said, so will not repeat them here, if you can not clear the point here.

2. Interface Design

To interface design, you must first open QtDesigner, then New Project, select Widget:


Then fill it controls like a drag, it is still very easy, with the main controls, including Label, Text Edit, Push Button, etc., you can modify various attributes on the right side of the control after double-clicked. The final design of the interface is as follows:


3. Generate Python code

After designing the interface, will generate results preserved, you will get a .ui file suffix. We still can not use this file directly, you need Python code into the job. At this point you need to use pyuic5 command, and you can not understand my point of view here on the blog post.


Fourth, the slot function

1. Introduction slot function

To design the function, you have to first know the slot function. Slot is an ordinary C ++ member function, the slot is a very important concept in Qt development software, In Qt signal is connected to the tank, we generally call slot function.

When using the signal, there are four parameters:

1) sender: issuing target signal;

2) signal: the signal emitted by the transmission target;

3) receiver: receiving a signal of the object;

4) slot: receiving object signal after receiving the calling function (function slots) required.

2. Use

(1) The first method

The first step, click on the “Edit Signals / Slots” in QtDesigner, and then left-click and drag the mouse button after another Label:


The second step, in the pop-up box, select the specific methods and functions.


This is very simple, but there are flaws, problems that can not be customized, you can only use a given method.

(2) The second method

Find the “Signal / Slot Editor” in the lower right corner and click, then you can click on the “+” to create a signal.


This approach allows us to customize, but not too easy, especially when you do not know exactly what method to achieve the time.

(3) A third method

Use connect () method, the argument passed to a method name. E.g:



Fifth, function realization

1. inherited function

Use pyuic5 generate py file defines a Ui_Form class, which contains definitions of the various controls and so on, if we want to add other features, and changes directly in the py file, then later to update the interface does not like It will be inconvenient, so the best way is to inherit Ui_Form class, and then add, modify inherited class. Reference code is as follows:

 1 import sys
 2 from PyQt5 import QtWidgets
 3 from typing.ui import Ui_Form
 6 class MyForm(Ui_Form, QtWidgets.QWidget):
 7     def __init__(self):
 8         super(MyForm, self).__init__()
 9         self.setupUi(self)
12 if __name__ == '__main__':
13     app = QtWidgets.QApplication(sys.argv)
14     my_form = MyForm()
16     sys.exit(app.exec_())

2. Hide Show controls

Sometimes we may need certain controls are hidden here briefly several ways.

1) setHidden (bool) Set the control is hidden;

2) hide () hides the controls;

These two methods will be completely hidden hide a control that does not retain control position occupied. But if you want to keep the location, you can use the following this method:

# 将透明度设置为0,达到隐藏且保留位置的目的
op = QtWidgets.QGraphicsOpacityEffect()

3. Analyzing the input content

To achieve this function needs to obtain the contents of the input box when you click the “Submit” button, and then compare the text given, finally returns the results displayed.

 1 def click(self):
 2     """
 3     点击按钮时调用
 4     :return: 
 5     """
 6     self.get_time()
 7     the_input = self.textEdit.toPlainText()
 8     #

Calculation accuracy rate

9 count = 0 10 for i in range(len(the_input)): 11 if the_input[i] == self.text[i]: 12 count += 1 13 accuracy = count / len(self.text) * 100 14 # print(accuracy) 15 self.show_label() 16 #

Setting message

17 info = "

A pity, your rate is correct:% .2f %%

" % accuracy if accuracy != 100 else "

Congratulations to the whole of it! Keep up the good oh!

" 18 self.info_lable.setText(info)

For me this lazy people, click the button such operations are laborious, it is best to add a shortcut, so much more convenient, you can use the following method to set:

# 设置快捷键


Sixth, operating results

The final run results screenshot below:


After submitting can also click on the next one to continue training:



The complete code has been uploaded to GitHub!

Leave a Reply