The 1th section of the Wxpython 2nd section draw a few Shapes 3rd section and then make a calculator 4th section finally realize a 2048 game
Experiment 1-recognize Wxpython one, experimental description 1. Environment Login
Automatic login without password, System user name Shiyanlou
2. Introduction to the Environment
This experiment environment uses the Ubuntu Linux environment with the desktop, the experiment will use the program on the desktop:
- LX Terminal (lxterminal): Linux command line terminal, Open will enter the bash environment, you can use the Linux command
- Firefox: Browser
- Sublime/gvim: Handy Editor
- git to get reference code
3. Use of the environment
Use the Gvim editor to enter the code and files required for the experiment, and use the LX Terminal (lxterminal) to run the required commands.
Ii. introduction of the course
Course Purpose:
熟悉实验环境了解wxpython能运行出一个窗口理解ClientDC和PaintDC的区别
Course environment has been installed Wxpython, supporting code Warehouse
http://git.shiyanlou.com/heibanke/shiyanlou_cs427
Get reference code to knock command git clone above the code warehouse
1. Wxpython
Wxpython is a python-based GUI library with the following advantages:
- Cross-platform, 32-bit Microsoft Windows, most Unix/linux,mac
- Open Source Free
- Simple to use
Official documentation Links
Good English tutorials are suitable for the system to learn the use of various controls.
<> good English and Chinese books, online easy to search the electronic version
2. Run a window
Direct instantiation
Import WX# Each Wxpython program must have a Wx.app object. App = Wx. APP ()# instantiates a frame"" "None: the parent window of the current window, if the current window is the topmost, then parent=none, if not the top-level window, its value is the name of the frame -1:id value,- 1 then the program will automatically generate a idpos: Position size: wide, tall and small there is style parameter style, do not fill the default is such a combination of WX. Maximize_box| Wx. Minimize_box| Wx. Resize_border|wx. system_menu| Wx. caption| Wx. Close_box you can remove a few look at the effect, such as style = WX. system_menu| Wx. caption| Wx. Close_box "" "frame = wx. Frame (None,-1, title=' wx_00_base.py ', pos= (),size= (Max.)# Center Processing # Frame. Centre ()# shows Frameframe. Show ()# enters the loop and waits for the window to respond to the app. Mainloop ()
3. How the frame subclass is defined
#coding=utf-8import wxclass Example(wx.Frame):def __init__(self, title): super(Example, self).__init__(None, title=title, size=(600, 400)) self.Centre() self.Show()if __name__=="__main__": app = wx.App() Example(‘Shapes‘) app.MainLoop()
The above two approaches are consistent.
4. Next draw a line
#-*-Coding:utf-8-*-import wxclass example (WX. Frame): def __init__none,title=title, size= (250, 150) self. Centre () self. Show () DC = wx. CLIENTDC (self) # draw a line with the X, Y, and X, y dc of the starting point. DrawLine (50, 60, 190, 60) if __name__ = ' __main__ ': app = wx. App () Example ( "line") app. Mainloop ()
Try to resize the window to a small, and then zoom in, what you will find. (lines are drawn only once without binding evt_paint, and if the resize window is small so that the full line is not displayed, and then the window is returned as it is, the lines that cannot be displayed cannot be restored.) )
5. Using PAINTDC
#-*-Coding:utf-8-*-Import WXClassExample(WX. Frame):Def__init__ (self, title): Super (Example, self). __init__ (none, Title=title, size= (250, 150)) # bind the action of the render window to OnPaint # so that when the resize window, the function self is called again. Bind (WX. Evt_paint, self. OnPaint) self. Centre () self. Show () # draw a line with a parameter of x, y of the starting point, X, y def onpaint (self, e): DC = wx. PAINTDC (self) DC. DrawLine (50, 60, 190, 60) if __name__ = ' __main__ ': app = wx. App () Example ( "line") app. Mainloop ()
After running, try to resize the window, after binding the event, because resize will call the binding function, so the line is there, no matter how you resize.
Finally we replace the drawing of a line with a number of lines to draw the DrawLine into DrawLines, for example:
dc.DrawLines(((20, 60), (100, 60), (100, 10),(20, 10), (20,60)))
The parameter is a single point, note that here the parameter format is a tuple list of point x, Y.
6. Save the Code
如果不熟悉git可以baidu看看教程。1. git clone 自己的仓库地址2. 实验代码拷过去以后用3. git add -A .4. git commit -am "your comment"5. git push origin master以后再用就clone自己的代码仓库就ok
Three. Summary
In this lesson we master the following points:
- About Wxpython
- Familiarity with the environment, especially git
- Can run out of Wxpython window
- Understand the differences between CLIENTDC and PAINTDC
- Draw Line
If you draw a polygon through specific points, it is inconvenient, and if you calculate the area of the polygon, compare the size of each polygon, and so on, it is more difficult to operate. In the next section, we use custom classes to draw various shapes to understand how classes are used.
Use Python to do 2048 games netease Cloud Classroom matching experiment class. Experience the fun of programming through the GUI.