可视化
Tofloor
poster avatar
z85525006
deepin
2012-02-29 08:20
Author
没有能力写的,收了。
Reply Favorite View the author
All Replies
hgyxbll
deepin
2012-03-01 04:53
#1
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (C) 2011 ~ 2012 邱海龙
  4. #
  5. # Author:     邱海龙 <356752238@qq.com>
  6. # Maintainer: 邱海龙 <356752238@qq.com>
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program.  If not, see .
  20. import gtk
  21. from box    import *
  22. from button import *
  23. import cairo
  24. from math import pi
  25. class Form(gtk.Window):
  26.    
  27.     def __init__(self):
  28.         gtk.Window.__init__(self)
  29.         
  30.         self.set_decorated(False) #无边框
  31.         self.set_default_size(500,300)
  32.         self.connect("size-allocate", draw_window_shape)
  33.         self.connect("destroy", self.destroy)
  34.         self.connect_after("expose-event", lambda w, e: expose_window_background(w, e,self._background))
  35.         #标题栏部分
  36.         self.box = EventBox()
  37.         self.layout_box = gtk.HBox()
  38.         self.box.add(self.layout_box)
  39.         
  40.         self.tit_hbox = gtk.HBox()
  41.         self.tit_align = gtk.Alignment()
  42.         self.tit_align.set(0.0, 0.0, 0.0, 0.0)
  43.         self.tit_align.add(self.tit_hbox)
  44.         self.layout_box.pack_start(self.tit_align,False,False)
  45.         
  46.         #添加标题栏 icon and text
  47.         try:
  48.             self.tit_icon_and_text = gtk.Button()
  49.             self.pixbuf = gtk.gdk.pixbuf_new_from_file("visual_python.png")
  50.             self.tit_icon_and_text.connect("expose-event",
  51.                                            self.tit_icon_and_text_expose)
  52.             self.tit_hbox.pack_start(self.tit_icon_and_text)
  53.         except:
  54.             print "图标加载失败... ..."        
  55.             
  56.         # Add drag event box.
  57.         self.drag_box = EventBox()
  58.         self.layout_box.pack_start(self.drag_box, True, True)
  59.         self.add_move_window_event(self.drag_box)
  60.         
  61.         
  62.         # Add button box.
  63.         self.button_box = gtk.HBox()
  64.         self.button_align = gtk.Alignment()
  65.         self.button_align.set(1.0, 0.0, 0.0, 0.0)
  66.         self.button_align.add(self.button_box)
  67.         self.layout_box.pack_start(self.button_align, False, False)
  68.         
  69.         #设置标题栏 按钮的变量.
  70.         self._theme  = False
  71.         self._menu   = False
  72.         self._min    = True
  73.         self._max    = True
  74.         self._close  = True
  75.         
  76.         self._text   = "Form"
  77.         self._icon   = "visual_python.png"
  78.         self._background = "background.jpg"
  79.         
  80.         #主题,菜单,缩小,扩大,关闭. 动态添加菜单.
  81.         self.button_list = {"theme":None,
  82.                             "menu":None,
  83.                             "min":Button(),
  84.                             "max":Button(),
  85.                             "close":Button()}
  86.         #动态加载按钮
  87.         self.load_button()
  88.         
  89.         # Init main box.
  90.         self.frame_vbox = gtk.VBox()
  91.         self.frame_hbox = gtk.HBox()
  92.         self.top_line = gtk.HBox()
  93.         self.top_line.set_size_request(-1, 1)
  94.         self.bottom_line = gtk.HBox()
  95.         self.bottom_line.set_size_request(-1, 1)
  96.         self.left_line = gtk.VBox()
  97.         self.left_line.set_size_request(1, -1)
  98.         self.right_line = gtk.VBox()
  99.         self.right_line.set_size_request(1, -1)
  100.         self.main_box = gtk.VBox()
  101.         self.frame_vbox.pack_start(self.top_line, False, False)
  102.         self.frame_vbox.pack_start(self.frame_hbox, True, True)
  103.         self.frame_vbox.pack_start(self.bottom_line, False, False)
  104.         self.frame_hbox.pack_start(self.left_line, False, False)
  105.         self.frame_hbox.pack_start(self.main_box, True, True)
  106.         self.frame_hbox.pack_start(self.right_line, False, False)
  107.         
  108.         #self.box.show_all()
  109.         
  110.         self.hbox = gtk.HBox()
  111.         self.fixed = gtk.Fixed()
  112.         self.hbox.pack_start(self.fixed)
  113.         
  114.         self.button = gtk.Button()
  115.         self.fixed.put(self.button,30,40)
  116.         
  117.         self.main_box.pack_start(self.box,True,True)
  118.         self.main_box.pack_start(self.hbox,True,True)
  119.         self.add(self.frame_vbox)
  120.     ##############################################
  121.     ###################方法部分####################   
  122.    
  123.     #背景#############        
  124.     @property   
  125.     def background(self):
  126.         return self._background
  127.    
  128.     @background.setter
  129.     def background(self, background_path):
  130.         self._background = background_path
  131.         self.queue_draw()
  132.         
  133.     @background.getter
  134.     def background(self):
  135.         return self._background                    
  136.     #################            
  137.     #图标#############
  138.     @property   
  139.     def icon(self):
  140.         return self._icon
  141.    
  142.     @icon.setter
  143.     def icon(self, icon_path):
  144.         self._icon = icon_path
  145.         self.tit_icon_and_text.queue_draw()
  146.         
  147.     @icon.getter
  148.     def icon(self):
  149.         return self._icon               
  150.     ################            
  151.     #标题text########
  152.     @property   
  153.     def text(self):
  154.         return self._text
  155.    
  156.     @text.setter
  157.     def text(self, _text):
  158.         self._text = _text
  159.         self.tit_icon_and_text.queue_draw()
  160.         
  161.     @text.getter
  162.     def text(self):
  163.         return self._text            
  164.     #################            
  165.     #主题#############
  166.     @property   
  167.     def theme(self):
  168.         return self._theme
  169.    
  170.     @theme.setter
  171.     def theme(self, theme_bool):
  172.         self._theme = theme_bool
  173.         self.set_titlebar_button_visible("theme", theme_bool)
  174.         
  175.     @theme.getter
  176.     def theme(self):
  177.         return self._theme            
  178.     #################            
  179.     #菜单#############
  180.     @property   
  181.     def menu(self):
  182.         return self._menu
  183.    
  184.     @menu.setter
  185.     def menu(self, menu_bool):
  186.         self._menu = menu_bool
  187.         self.set_titlebar_button_visible("menu", menu_bool)
  188.         
  189.     @menu.getter
  190.     def menu(self):
  191.         return self._menu            
  192.     ##################
  193.     #缩小##############
  194.     @property   
  195.     def min(self):
  196.         return self._min
  197.    
  198.     @min.setter
  199.     def min(self, min_bool):
  200.         self._min = min_bool
  201.         self.set_titlebar_button_visible("min", min_bool)
  202.         
  203.     @min.getter
  204.     def min(self):
  205.         return self._min
  206.     ##################
  207.     #扩大##############
  208.     @property   
  209.     def max(self):
  210.         return self._max
  211.    
  212.     @max.setter
  213.     def max(self, max_bool):
  214.         self._max = max_bool
  215.         self.set_titlebar_button_visible("max", max_bool)
  216.         
  217.     @max.getter
  218.     def max(self):
  219.         return self._max
  220.     ###################
  221.     #关闭###############
  222.     @property   
  223.     def close(self):
  224.         return self._close
  225.    
  226.     @close.setter
  227.     def close(self, close_bool):
  228.         self._close = close_bool
  229.         self.set_titlebar_button_visible("close", close_bool)
  230.         
  231.     @close.getter
  232.     def close(self):
  233.         return self._close
  234.    
  235.     #############################
  236.     #############################
  237.     def destroy_button(self):
  238.         '''销毁所有不为None的控件'''
  239.         for widget in self.button_list:
  240.             if self.button_list[widget] != None:
  241.                 self.button_list[widget].destroy()
  242.                
  243.     def load_button(self):
  244.         '''加载不为None的控件'''
  245.         for widget in self.button_list:
  246.             if self.button_list[widget] != None:
  247.                 self.button_box.pack_end(self.button_list[widget], False, False)
  248.                
  249.     def set_titlebar_button_visible(self,button_type, button_bool):        
  250.         '''设置标题栏的按钮是否显示(False 隐藏.True 显示)'''
  251.         if button_bool:
  252.             if not self.button_list[button_type]:
  253.                 self.button_list[button_type] = Button()
  254.                 self.destroy_button()
  255.                 self.load_button()
  256.         else:
  257.             if self.button_list[button_type]:
  258.                 self.button_list[button_type].destroy()
  259.                 self.button_list[button_type] = None
  260.    
  261.    
  262.     def add_move_window_event(self, widget):   
  263.         widget.connect('button-press-event', lambda w, e: self.move_window(w, e))
  264.         
  265.     def move_window(self,widget, event):
  266.        '''移动窗体.'''
  267.        #判断是否为鼠标左键和双击.
  268.        if event.button == 1 and event.type == gtk.gdk._2BUTTON_PRESS:
  269.             if self.window.get_state() == gtk.gdk.WINDOW_STATE_MAXIMIZED:
  270.                 self.window.unmaximize()
  271.             else:
  272.                 self.window.maximize()
  273.                
  274.        self.begin_move_drag(
  275.             event.button,
  276.             int(event.x_root),
  277.             int(event.y_root),
  278.             event.time)   
  279.       
  280.        return True               
  281.    
  282.     def tit_icon_and_text_expose(self, widget, event):
  283.         '''绘制标题栏图标'''
  284.         cr = widget.window.cairo_create()
  285.         rect = widget.allocation
  286.         x,y,w,h = rect.x, rect.y, rect.width, rect.height
  287.         #标题图标16x16
  288.         if self.pixbuf.get_width() <= 16 and self.pixbuf.get_height() <=16:
  289.             cr.set_source_pixbuf(self.pixbuf,x+2,y)
  290.             cr.paint_with_alpha(0.8)
  291.         cr.select_font_face("文泉驿微米黑",cairo.FONT_SLANT_NORMAL,
  292.                             cairo.FONT_WEIGHT_NORMAL)
  293.         cr.set_font_size(12)
  294.         cr.set_source_rgb(0,0,0)
  295.         cr.move_to(x+22.,y+12)
  296.         cr.show_text(self._text)
  297.         return True
  298.         
  299. #下面是王勇的UI库.
  300. def draw_window_shape(widget, rect):
  301.     '''Update shape.'''
  302.     if rect.width > 0 and rect.height > 0:
  303.         # Init.
  304.         w, h = rect.width, rect.height
  305.         bitmap = gtk.gdk.Pixmap(None, w, h, 1)
  306.         cr = bitmap.cairo_create()
  307.         
  308.         # Clear the bitmap
  309.         cr.set_source_rgb(0.0, 0.0, 0.0)
  310.         cr.set_operator(cairo.OPERATOR_CLEAR)
  311.         cr.paint()
  312.         
  313.         # Draw our shape into the bitmap using cairo
  314.         cr.set_source_rgb(1.0, 1.0, 1.0)
  315.         cr.set_operator(cairo.OPERATOR_OVER)
  316.         draw_round_rectangle(cr, 0, 0, w, h, 6)
  317.         cr.fill()
  318.         # Shape with given mask.
  319.         widget.shape_combine_mask(bitmap, 0, 0)
  320.         
  321.         # Redraw.
  322.         widget.queue_draw()     # import to redraw interface        
  323. def draw_round_rectangle(cr, x, y, width, height, r):
  324.     '''Draw round rectangle.'''
  325.     # Top side.
  326.     cr.move_to(x + r, y)
  327.     cr.line_to(x + width - r, y)
  328.    
  329.     # Top-right corner.
  330.     cr.arc(x + width - r, y + r, r, pi * 3 / 2, pi * 2)
  331.    
  332.     # Right side.
  333.     cr.line_to(x + width, y + height - r)
  334.    
  335.     # Bottom-right corner.
  336.     cr.arc(x + width - r, y + height - r, r, 0, pi / 2)
  337.    
  338.     # Bottom side.
  339.     cr.line_to(x + r, y + height)
  340.    
  341.     # Bottom-left corner.
  342.     cr.arc(x + r, y + height - r, r, pi / 2, pi)
  343.    
  344.     # Left side.
  345.     cr.line_to(x, y + r)
  346.    
  347.     # Top-left corner.
  348.     cr.arc(x + r, y + r, r, pi, pi * 3 / 2)
  349.     # Close path.
  350.     cr.close_path()
  351. #画背景
  352. def expose_window_background(widget, event,background_path):
  353.     '''Expose background.'''
  354.     alpha=0.0
  355.     mask_dcolor="windowMask"
  356.     # Init.
  357.     cr = widget.window.cairo_create()
  358.     pixbuf = gtk.gdk.pixbuf_new_from_file(background_path)
  359.     rect = widget.allocation
  360.    
  361.     # Clear color to transparent window.
  362.     cr.set_source_rgba(0.0, 0.0, 0.0, 0.0)
  363.     cr.set_operator(cairo.OPERATOR_SOURCE)
  364.     cr.paint()
  365.         
  366.     # Draw background.
  367.     #draw_pixbuf(cr, pixbuf, rect.x, rect.y, 0.8)
  368.     cr.set_source_pixbuf(pixbuf,rect.x,rect.y)
  369.     cr.paint_with_alpha(0.8)
  370.    
  371.     # Draw mask.
  372.     cr.set_line_width(0.5)
  373.     cr.set_source_rgb(0,0,0)
  374.     cr.rectangle(0, 0, rect.width, rect.height)   
  375.     cr.paint_with_alpha(alpha)
  376.    
  377.     # Draw frame light.
  378.     cr.set_source_rgba(0,0,0,0.8)
  379.     cr.set_operator(cairo.OPERATOR_OVER)
  380.     draw_round_rectangle(cr, 1, 1, rect.width - 2, rect.height - 2, 6)
  381.     cr.stroke()
  382.    
  383.     # Draw frame.
  384.     cr.set_source_rgba(0,0,0,0.9)
  385.     cr.set_operator(cairo.OPERATOR_OVER)
  386.     draw_round_rectangle(cr, 0, 0, rect.width, rect.height, 6)
  387.     cr.stroke()
  388.    
  389.     # Propagate expose.
  390.     propagate_expose(widget, event)
  391.     return True
  392. def propagate_expose(widget, event):
  393.     '''Propagate expose to children.'''
  394.     if "get_child" in dir(widget) and widget.get_child() != None:
  395.         widget.propagate_expose(widget.get_child(), event)
  396.         
  397. if __name__ == "__main__":
  398.    
  399.     win = Form()
  400.     print win.icon
  401.     win.text  =  "可视化开发环境 visual python"
  402.     win.close = False
  403.     win.background = "b1.jpg"
  404.     win.show_all()
  405.     gtk.main()
Copy the Code

只需要輕鬆的設置.
.close = False   .max = False 就关掉不要要的标题东西.
设置为真后,有可以显示出来.
.background 轻松设置背景图.
很像C#,易语言.
有时间后,就将按钮的图片设置也傻瓜化.原状态, 鼠标划过状态, 鼠标按下.
还有一些等等控件.
下面有事件再弄弄那个可视化开发工具.

界面设计器源码.
界面设计器.tar.gz
这个源代码只是我想试试,能否可以实现可视化开发环境.
现在正准备将 这个傻瓜的UI库和可视化合位一体.
凡事不理论啊,实践出点东西才放屁....
:
支持!!!
Reply View the author
z85525006
deepin
2012-03-01 05:32
#2
地方上方的
Reply View the author