Base pyqt4, simple web app framwork

Source: Internet
Author: User

For a long timeProgramEvery day, there is a black box, and only log files are output. Occasionally, you need to simulate a client test, or write several lines of PHPCodeIn the browser, either write a small Python script and execute it in the shell. After writing some small scripts for testing, I became more and more fond of this script testing method. Think of the test case, the script can be tested after three, five, and two. A dozen or more script tests have been written. In fact, I am not familiar with PHP and python, but I only know a little about it. In fact, most of the problems can be easily solved by Google. I don't need to use Shenma's advanced features. Haha.

The tests of recent projects have made me hard. I need to develop GUI programs! As a C ++ programmer on a Linux server, brother has always been disdainto write GUI and is also idle (he is not very comfortable with himself ). We developed a map-based game. Now we want to test stability to simulate random movement of N users, and test server performance. When users are relatively concentrated in a certain area of the map, the response efficiency of the server. This is the most important requirement currently. We need to develop a GUI program to display the movement of all people on the map. The solution is to narrow down the entire map to only one screen and display the user as a red dot. It can dynamically add users and display the load map of each region of the server in real time. In addition to this requirement, you have also encountered some requirements that require GUI tools, such as checking the Excel defined by the task, and checking the XML validity of the map configuration. These tools are currently available through web or flash, but in fact these tools are best maintained by background programmers. After all, some restrictions and checks on background development are the most clear.

For the first time in N years, I want to write a GUI program. If you don't want to write it, it's amazing. Haha! That is, a general point !! The requirements for sorting are as follows:

    • Frequently used tool programs must be modified. So GUI tool programs must be written in scripts.
    • Most of the tools used are development, planning, and O & M. Do you know they all use the Shenma system? Ordinary young people use Windows, literary and art young people use Mac, second force young people use Linux, so GUI tools also need cross-platform
    • What is the easiest GUI to write in the world? My answer is "html". If I write Web through JB in my university, and my brother can use HTML + jquery, the efficiency is definitely leveraged. However, if you directly write a PHP page, there are some defects. First, you cannot run it for a long time. Second, normal HTML + jquery cannot access the local file system, you cannot initiate a socket to connect to the server.

After two steps, the rest are Lua and python, but the Lua library is too small to be removed. In addition, after a hard Google + demo test, Python + widgets, and Python + QT are left. At the beginning, wxpython was selected, and a common HTML page was created after several hours. At this time, the tragic discovery tower did not support CSS and JavaScript, and even forms were not supported, I expanded the support button for it myself, but I was reluctant to support JavaScript. So decisively discards the code.

Finally, pyqt became my final choice. After reading the QT library a little bit, I couldn't help but sigh that this library is too good at JB, and the foreign experts are awesome. This awesome library is open-source. Qt contains N encapsulation in many aspects. Specifically, WebKit is used to render HTML. Chrome and Safari are all rendered based on the WebKit engine, people's basic databases are doing well, and they have accumulated technical skills for a long time. Why didn't we see Chinese people engaged in the Shenma rendering engine. This is really a good thing for the benefit of mankind. I have always thought that the most awesome software is like QT and WebKit. The people who develop this software are awesome. Even those who use this software feel that they can use this awesome software, I feel awesome. In addition to QT and WebKit, there are MySQL, Linux, memcache, and GCC. I have seen n people familiar with Oracle, but I have never seen Oracle authors. It should be more awesome.

My implementation is described below: mainly based on the window platform (Note: It is currently an ordinary youth)

1. Install Python

Download the installation package from window. Next Step: python.org. We recommend that you download the python2.7 32-bit version and the recommended connection.

Python-2.7.1.zip

Linux: one-click installation of Yum/Apt

2. Install pyqt4

Download the corresponding installation package in windows. It is recommended that you download the package corresponding to the Python version.

PyQt-Py2.7-x86-gpl-4.9-1.exe Windows 32 bit Installer

Http://www.riverbankcomputing.co.uk/software/pyqt/download

Linux Yum/apt or download the tar package to install the package by yourself. The second force is always troublesome for young people.

3. Download the even sample code:

SVN Co https://ffown.googlecode.com/svn/trunk/python

4. Run

Double-click main. PY can be run and run as follows, mainly including examples of downloading images through Google, examples of jquery control buttons, examples of accessing the file system through python, examples of URL redirection, and examples of HTML5 animation.

 

 

5. Detailed description of the framework:

To use pyqt to implement a browser, you only need the following lines of code: see pylib/simple. py

Import sys
From pyqt4.qtcore import *
From pyqt4.qtgui import *
From pyqt4.qtwebkit import *

APP = qapplication (SYS. argv)

Web = qwebview ()
Web. Load (qurl ("http://www.cnblogs.com/zhiranok "))
Web. Show ()

Sys.exit(app.exe C _())

I encapsulate pyqtwebkit into ffqt. py in pylib.

Different from above, I have encapsulated several functions for JavaScript to call. 2. You can use the index.html] file in the HTML directory. To modify gui, you only need to modify the index.html file.

Class pythonjs (qobject): url = [] _ pyqtsignals _ = ("contentchanged (const qstring &)") @ pyqtsignature ("", result = "qstring ") def get_image_url (Self): If Len (self. URL) = 0: search_url = 'HTTP: // ffown.sinaapp.com/get_image_url.php' search_ret = eval (urllib. urlopen (search_url ). read () self. url = search_ret DEST = self. URL [random. randint (0,100) % Len (self. URL)] Return DEST @ pyqtsignature ("qstring", R Esult = "qstring") def readfile (self, PATH): ret = "" try: F = open (path, "R") ret = f. read () F. close () failed T: ret = "file not exist! "+ Pathtraceback. print_exc () return ret @ pyqtsignature ("qstring", result = "qstring") def select_file (self, ext = "*"): ret = qfiledialog. getopenfilename (none, "", ext, "filedialog") return re

Here, pythonjs is an extension function library encapsulated by JavaScript. Each member method javascript can be called directly. For example, readfile for reading file content is called in Javascript

Python. readfile (file_path)

In this way, you can use python to access the file system, socket connection, and database. Using HTML and jquery, you can easily write a very practical GUI program.

Please try it, Welcome young people in literature and art, second forced youth, ordinary youth to test and report bugs.

 

 

 

 

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.