Follow the ghost Brother to learn PyQtPart.4, ghost Brother pyqtpart.4

Source: Internet
Author: User

Follow the ghost Brother to learn PyQtPart.4, ghost Brother pyqtpart.4

We have been learning for the past two days since the Mid-Autumn Festival holiday. After studying the previous three articles, we can develop a simple tool for decompiling apk.

To make it easier to use our own tools, we will process the search_file button in the third article. We will drag the file to the QLineEdit control to display the file path, you don't need to open the window to select it, which is more convenient for our operations.

 

0x1: process drag events

Drag and drop events. After one night's query and a look at the Java tool, we finally found two events: dragEnterEvent and dropEvent. For a detailed introduction to these two events, you can search for them by google.

To use this event, we need to override the QLineEdit control and set setAcceptDrops to True in its init method to receive drag events.

So now the question is how can we get the path of the dragged file?

There are some methods for querying mimeData. After testing, the urls () attribute is still close to what we need, but we still need to process it to get the file path.

The Code is as follows:


Def dropEvent (self, event): ### obtain the path of the drag-and-drop file st = str (event. mimeData (). urls () print (st) ### no good APIs are found here, so the urls attribute of mimeData () is used, and text () is not useful, so the following is the string truncation, to restore path a = '//' B = ')] 'num2 = st. index (B)-1 num = st. index (a) + 3 st = st [num: num2] # define global variables here, and drag the path of the file or folder to global APK_STR = st self. setText (st)



0x2: redo the layout file.

The detailed code is as follows and detailed comments are provided:


QWidget. _ init _ (self, parent) self. setWindowTitle ('android _ APK ') ### first line self.apk _ decode = QPushButton ("Apk_Decode") self.apk _ path_edit = MyEditText (self) ## second line, three buttons: self.apk _ build = QPushButton ("Apk_Build") self.apk _ build_edit = MyEditText () ### self in the third line. sign = QPushButton ("Apk_Sign") self.apk _ sign_edit = MyEditText () # Row 4 self. shootcut = QPushButton ("Shoot_Cut") self. shootcut_text = QLabel ("please keep moile with usb") ### layout file writing, four rows and two columns grid = QGridLayout () grid. setSpacing (10) Empty _ decode, 1, 0) grid.addWidget(self.apk _ path_edit, 1, 1) Empty _ build, 2, 0) grid.addWidget(self.apk _ build_edit, 2, 1) grid. addWidget (self. sign, 3, 0) grid.addWidget(self.apk _ sign_edit, 3, 1) grid. addWidget (self. shootcut, 4, 0) grid. addWidget (self. shootcut_text,) ### set the height and width of the window and place the gridview in the large layout self. setLayout (grid) self. resize (450,300)


As follows:





0x3: Click events of the integrated button

 

The Decompilation button event has been completed in the previous article, so here we just need to write back compilation, signature, and final screenshot event.



Def apkD (self): ### global variables defined in the openFIle method above, print the path to see if they are correct #### print (APK_STR) ### apktool command ##### apktodd = 'java-jar apktool. jar d' + APK_STR ### call the OS system command to execute cmd commands ### OS. system (apktodd) def apkB (self): ### global variables defined in the openFIle method above, print the path to see if it is correct ##### print (APK_STR) ### apktool command ##### apktodd = 'java-jar apktool. jar B '+ APK_STR ### call the OS system command to execute cmd commands ### OS. system (apktodd) def apkS (self): apkSign = 'java-jar signapk. jar testkey. x509.pem testkey. pk8 '+ APK_STR + 'signed_apk.apk' print apkSign ### call the OS system command to execute cmd commands ### OS. system (apkSign) def shootCut (self): pre_com = 'adb wait-for-device' OS. system (pre_com) ### save it to fir_com = 'adb shell screencap-p/data/local/tmp/tmp.png 'print (fir_com) OS. system (fir_com) ### current time, accurate to seconds, to save the image name timestamp = time. strftime ('% Y-% m-% d-% H-% M-% s', time. localtime (time. time () path = OS. getcwd () ### export it to sec_com = 'adb pull/data/local/tmp/tmp.png '+ path +' \ 'hour timestamp;'.png 'print (sec_com) OS. system (sec_com) thi_com = 'adb shell rm/data/local/tmp/tmp.png 'OS. system (thi_com)



0x4: complete code

The complete code is attached below. If you are interested, test it directly. Remember to configure the pyqt development environment first.


#-*-Coding: UTF-8-*-from PyQt4.QtGui import * from PyQt4.QtCore import * import sysimport osimport timeclass MainLayout (QWidget): def _ init _ (self, parent = None): QWidget. _ init _ (self, parent) self. setWindowTitle ('android _ APK ') ### first line self.apk _ decode = QPushButton ("Apk_Decode") self.apk _ path_edit = MyEditText (self) ## second line, three buttons: self.apk _ build = QPushButton ("Apk_Build") self.apk _ build_edit = MyEditText () ### self in the third line. sign = QPushButton ("Apk_Sign") self.apk _ sign_edit = MyEditText () # Row 4 self. shootcut = QPushButton ("Shoot_Cut") self. shootcut_text = QLabel ("please keep moile with usb") ### click The Decompilation button event self.connect(self.apk_decode,signal('clicked('{'},self.apk D) ### click The Decompilation button upload B) ### signature button click event self.connect(self.sign,signal('clicked('{'},self.apk S) ### screenshot button click event self. connect (self. shootcut, SIGNAL ('clicked () '), self. shootCut) ### write the layout file, four rows and two columns grid = QGridLayout () grid. setSpacing (10) Empty _ decode, 1, 0) grid.addWidget(self.apk _ path_edit, 1, 1) Empty _ build, 2, 0) grid.addWidget(self.apk _ build_edit, 2, 1) grid. addWidget (self. sign, 3, 0) grid.addWidget(self.apk _ sign_edit, 3, 1) grid. addWidget (self. shootcut, 4, 0) grid. addWidget (self. shootcut_text,) ### set the height and width of the window and place the gridview in the large layout self. setLayout (grid) self. resize (450,300) ''' def openFile (self): # Call the QfileDialog method to open the file selection box s = QFileDialog. getOpenFileName (self, "Open file dialog", "/", "Apk files (*. apk) ") # define the global variable, used to receive the path of the apk to be decomcompiled: global APK_STR = str (s) self.apk _ path_edit.setText (APK_STR) ''' def apkD (self): ### global variables defined in the openFIle method above, print the path to see if they are correct #### print (APK_STR) ### apktool command ##### apktodd = 'java-jar apktool. jar d' + APK_STR ### call the OS system command to execute cmd commands ### OS. system (apktodd) def apkB (self): ### global variables defined in the openFIle method above, print the path to see if it is correct ##### print (APK_STR) ### apktool command ##### apktodd = 'java-jar apktool. jar B '+ APK_STR ### call the OS system command to execute cmd commands ### OS. system (apktodd) def apkS (self): apkSign = 'java-jar signapk. jar testkey. x509.pem testkey. pk8 '+ APK_STR + 'signed_apk.apk' print apkSign ### call the OS system command to execute cmd commands ### OS. system (apkSign) def shootCut (self): pre_com = 'adb wait-for-device' OS. system (pre_com) ### save it to fir_com = 'adb shell screencap-p/data/local/tmp/tmp.png 'print (fir_com) OS. system (fir_com) ### current time, accurate to seconds, to save the image name timestamp = time. strftime ('% Y-% m-% d-% H-% M-% s', time. localtime (time. time () path = OS. getcwd () ### export it to sec_com = 'adb pull/data/local/tmp/tmp.png '+ path +' \ 'hour timestamp;'.png 'print (sec_com) OS. system (sec_com) thi_com = 'adb shell rm/data/local/tmp/tmp.png 'OS. system (thi_com) class MyEditText (QLineEdit): def _ init _ (self, parent = None): super (MyEditText, self ). _ init _ (parent) # initialize init here to accept the drag event self. setAcceptDrops (True) ### override the two methods for dragging files. def dragEnterEvent (self, event): event. accept () def dropEvent (self, event): ### obtain the path of the drag-and-drop file st = str (event. mimeData (). urls () print (st) ### no good APIs are found here, so the urls attribute of mimeData () is used, and text () is not useful, so the following is the string truncation, to restore path a = '//' B = ')] 'num2 = st. index (B)-1 num = st. index (a) + 3 st = st [num: num2] # define global variables here, and drag the path of the file or folder to global APK_STR = st self. setText (st) ### main entry of the program if _ name _ = "_ main _": app = QApplication (sys. argv) qb = MainLayout () qb. show () sys.exit(app.exe c _())




The previous articles are basically available and will not be posted here.

Note:

1. The Decompilation uses the apktool. jar command, so you have to put it in the directory corresponding to the. py file.

2. The signature uses the public key of google. Therefore, the file must be in the same directory.

3. screenshots must ensure that the mobile phone is connected to usb normally and the adb process is not occupied. Therefore, if the reader has its own adb compiled, this function is best, directly run the cmd script to put the commands in the above method

4. These four articles only record the learning process of PyQt. Readers can expand and develop it at will, and create an IDE tool set to search, modify, save, and automate some tools.

Conclusion:

Although I didn't go out for a few days during the Mid-Autumn Festival, I still made some progress in learning PyQt from the beginning. There must be a lot of omissions in code writing, and more optimizations can be made in the corresponding functions. If you are interested, please continue to work on it.

Encourage yourself in one sentence:

Diligence and self-help are good training.

 

I uploaded the attachment to the Baidu Network Disk:


Http://pan.baidu.com/s/1o6sONoQ





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.