hgyxbll
deepin
2012-03-01 04:53 Copy the Code
- #! /usr/bin/env python
- # -*- coding: utf-8 -*-
- # Copyright (C) 2011 ~ 2012 邱海龙
- #
- # Author: 邱海龙 <356752238@qq.com>
- # Maintainer: 邱海龙 <356752238@qq.com>
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see .
- import gtk
- from box import *
- from button import *
- import cairo
- from math import pi
- class Form(gtk.Window):
- def __init__(self):
- gtk.Window.__init__(self)
- self.set_decorated(False) #无边框
- self.set_default_size(500,300)
- self.connect("size-allocate", draw_window_shape)
- self.connect("destroy", self.destroy)
- self.connect_after("expose-event", lambda w, e: expose_window_background(w, e,self._background))
- #标题栏部分
- self.box = EventBox()
- self.layout_box = gtk.HBox()
- self.box.add(self.layout_box)
- self.tit_hbox = gtk.HBox()
- self.tit_align = gtk.Alignment()
- self.tit_align.set(0.0, 0.0, 0.0, 0.0)
- self.tit_align.add(self.tit_hbox)
- self.layout_box.pack_start(self.tit_align,False,False)
- #添加标题栏 icon and text
- try:
- self.tit_icon_and_text = gtk.Button()
- self.pixbuf = gtk.gdk.pixbuf_new_from_file("visual_python.png")
- self.tit_icon_and_text.connect("expose-event",
- self.tit_icon_and_text_expose)
- self.tit_hbox.pack_start(self.tit_icon_and_text)
- except:
- print "图标加载失败... ..."
- # Add drag event box.
- self.drag_box = EventBox()
- self.layout_box.pack_start(self.drag_box, True, True)
- self.add_move_window_event(self.drag_box)
- # Add button box.
- self.button_box = gtk.HBox()
- self.button_align = gtk.Alignment()
- self.button_align.set(1.0, 0.0, 0.0, 0.0)
- self.button_align.add(self.button_box)
- self.layout_box.pack_start(self.button_align, False, False)
- #设置标题栏 按钮的变量.
- self._theme = False
- self._menu = False
- self._min = True
- self._max = True
- self._close = True
- self._text = "Form"
- self._icon = "visual_python.png"
- self._background = "background.jpg"
- #主题,菜单,缩小,扩大,关闭. 动态添加菜单.
- self.button_list = {"theme":None,
- "menu":None,
- "min":Button(),
- "max":Button(),
- "close":Button()}
- #动态加载按钮
- self.load_button()
- # Init main box.
- self.frame_vbox = gtk.VBox()
- self.frame_hbox = gtk.HBox()
- self.top_line = gtk.HBox()
- self.top_line.set_size_request(-1, 1)
- self.bottom_line = gtk.HBox()
- self.bottom_line.set_size_request(-1, 1)
- self.left_line = gtk.VBox()
- self.left_line.set_size_request(1, -1)
- self.right_line = gtk.VBox()
- self.right_line.set_size_request(1, -1)
- self.main_box = gtk.VBox()
- self.frame_vbox.pack_start(self.top_line, False, False)
- self.frame_vbox.pack_start(self.frame_hbox, True, True)
- self.frame_vbox.pack_start(self.bottom_line, False, False)
- self.frame_hbox.pack_start(self.left_line, False, False)
- self.frame_hbox.pack_start(self.main_box, True, True)
- self.frame_hbox.pack_start(self.right_line, False, False)
- #self.box.show_all()
- self.hbox = gtk.HBox()
- self.fixed = gtk.Fixed()
- self.hbox.pack_start(self.fixed)
- self.button = gtk.Button()
- self.fixed.put(self.button,30,40)
- self.main_box.pack_start(self.box,True,True)
- self.main_box.pack_start(self.hbox,True,True)
- self.add(self.frame_vbox)
- ##############################################
- ###################方法部分####################
- #背景#############
- @property
- def background(self):
- return self._background
- @background.setter
- def background(self, background_path):
- self._background = background_path
- self.queue_draw()
- @background.getter
- def background(self):
- return self._background
- #################
- #图标#############
- @property
- def icon(self):
- return self._icon
- @icon.setter
- def icon(self, icon_path):
- self._icon = icon_path
- self.tit_icon_and_text.queue_draw()
- @icon.getter
- def icon(self):
- return self._icon
- ################
- #标题text########
- @property
- def text(self):
- return self._text
- @text.setter
- def text(self, _text):
- self._text = _text
- self.tit_icon_and_text.queue_draw()
- @text.getter
- def text(self):
- return self._text
- #################
- #主题#############
- @property
- def theme(self):
- return self._theme
- @theme.setter
- def theme(self, theme_bool):
- self._theme = theme_bool
- self.set_titlebar_button_visible("theme", theme_bool)
- @theme.getter
- def theme(self):
- return self._theme
- #################
- #菜单#############
- @property
- def menu(self):
- return self._menu
- @menu.setter
- def menu(self, menu_bool):
- self._menu = menu_bool
- self.set_titlebar_button_visible("menu", menu_bool)
- @menu.getter
- def menu(self):
- return self._menu
- ##################
- #缩小##############
- @property
- def min(self):
- return self._min
- @min.setter
- def min(self, min_bool):
- self._min = min_bool
- self.set_titlebar_button_visible("min", min_bool)
- @min.getter
- def min(self):
- return self._min
- ##################
- #扩大##############
- @property
- def max(self):
- return self._max
- @max.setter
- def max(self, max_bool):
- self._max = max_bool
- self.set_titlebar_button_visible("max", max_bool)
- @max.getter
- def max(self):
- return self._max
- ###################
- #关闭###############
- @property
- def close(self):
- return self._close
- @close.setter
- def close(self, close_bool):
- self._close = close_bool
- self.set_titlebar_button_visible("close", close_bool)
- @close.getter
- def close(self):
- return self._close
- #############################
- #############################
- def destroy_button(self):
- '''销毁所有不为None的控件'''
- for widget in self.button_list:
- if self.button_list[widget] != None:
- self.button_list[widget].destroy()
- def load_button(self):
- '''加载不为None的控件'''
- for widget in self.button_list:
- if self.button_list[widget] != None:
- self.button_box.pack_end(self.button_list[widget], False, False)
- def set_titlebar_button_visible(self,button_type, button_bool):
- '''设置标题栏的按钮是否显示(False 隐藏.True 显示)'''
- if button_bool:
- if not self.button_list[button_type]:
- self.button_list[button_type] = Button()
- self.destroy_button()
- self.load_button()
- else:
- if self.button_list[button_type]:
- self.button_list[button_type].destroy()
- self.button_list[button_type] = None
- def add_move_window_event(self, widget):
- widget.connect('button-press-event', lambda w, e: self.move_window(w, e))
- def move_window(self,widget, event):
- '''移动窗体.'''
- #判断是否为鼠标左键和双击.
- if event.button == 1 and event.type == gtk.gdk._2BUTTON_PRESS:
- if self.window.get_state() == gtk.gdk.WINDOW_STATE_MAXIMIZED:
- self.window.unmaximize()
- else:
- self.window.maximize()
- self.begin_move_drag(
- event.button,
- int(event.x_root),
- int(event.y_root),
- event.time)
- return True
- def tit_icon_and_text_expose(self, widget, event):
- '''绘制标题栏图标'''
- cr = widget.window.cairo_create()
- rect = widget.allocation
- x,y,w,h = rect.x, rect.y, rect.width, rect.height
- #标题图标16x16
- if self.pixbuf.get_width() <= 16 and self.pixbuf.get_height() <=16:
- cr.set_source_pixbuf(self.pixbuf,x+2,y)
- cr.paint_with_alpha(0.8)
- cr.select_font_face("文泉驿微米黑",cairo.FONT_SLANT_NORMAL,
- cairo.FONT_WEIGHT_NORMAL)
- cr.set_font_size(12)
- cr.set_source_rgb(0,0,0)
- cr.move_to(x+22.,y+12)
- cr.show_text(self._text)
- return True
- #下面是王勇的UI库.
- def draw_window_shape(widget, rect):
- '''Update shape.'''
- if rect.width > 0 and rect.height > 0:
- # Init.
- w, h = rect.width, rect.height
- bitmap = gtk.gdk.Pixmap(None, w, h, 1)
- cr = bitmap.cairo_create()
- # Clear the bitmap
- cr.set_source_rgb(0.0, 0.0, 0.0)
- cr.set_operator(cairo.OPERATOR_CLEAR)
- cr.paint()
- # Draw our shape into the bitmap using cairo
- cr.set_source_rgb(1.0, 1.0, 1.0)
- cr.set_operator(cairo.OPERATOR_OVER)
- draw_round_rectangle(cr, 0, 0, w, h, 6)
- cr.fill()
- # Shape with given mask.
- widget.shape_combine_mask(bitmap, 0, 0)
- # Redraw.
- widget.queue_draw() # import to redraw interface
- def draw_round_rectangle(cr, x, y, width, height, r):
- '''Draw round rectangle.'''
- # Top side.
- cr.move_to(x + r, y)
- cr.line_to(x + width - r, y)
- # Top-right corner.
- cr.arc(x + width - r, y + r, r, pi * 3 / 2, pi * 2)
- # Right side.
- cr.line_to(x + width, y + height - r)
- # Bottom-right corner.
- cr.arc(x + width - r, y + height - r, r, 0, pi / 2)
- # Bottom side.
- cr.line_to(x + r, y + height)
- # Bottom-left corner.
- cr.arc(x + r, y + height - r, r, pi / 2, pi)
- # Left side.
- cr.line_to(x, y + r)
- # Top-left corner.
- cr.arc(x + r, y + r, r, pi, pi * 3 / 2)
- # Close path.
- cr.close_path()
- #画背景
- def expose_window_background(widget, event,background_path):
- '''Expose background.'''
- alpha=0.0
- mask_dcolor="windowMask"
- # Init.
- cr = widget.window.cairo_create()
- pixbuf = gtk.gdk.pixbuf_new_from_file(background_path)
- rect = widget.allocation
- # Clear color to transparent window.
- cr.set_source_rgba(0.0, 0.0, 0.0, 0.0)
- cr.set_operator(cairo.OPERATOR_SOURCE)
- cr.paint()
- # Draw background.
- #draw_pixbuf(cr, pixbuf, rect.x, rect.y, 0.8)
- cr.set_source_pixbuf(pixbuf,rect.x,rect.y)
- cr.paint_with_alpha(0.8)
- # Draw mask.
- cr.set_line_width(0.5)
- cr.set_source_rgb(0,0,0)
- cr.rectangle(0, 0, rect.width, rect.height)
- cr.paint_with_alpha(alpha)
- # Draw frame light.
- cr.set_source_rgba(0,0,0,0.8)
- cr.set_operator(cairo.OPERATOR_OVER)
- draw_round_rectangle(cr, 1, 1, rect.width - 2, rect.height - 2, 6)
- cr.stroke()
- # Draw frame.
- cr.set_source_rgba(0,0,0,0.9)
- cr.set_operator(cairo.OPERATOR_OVER)
- draw_round_rectangle(cr, 0, 0, rect.width, rect.height, 6)
- cr.stroke()
- # Propagate expose.
- propagate_expose(widget, event)
- return True
- def propagate_expose(widget, event):
- '''Propagate expose to children.'''
- if "get_child" in dir(widget) and widget.get_child() != None:
- widget.propagate_expose(widget.get_child(), event)
- if __name__ == "__main__":
- win = Form()
- print win.icon
- win.text = "可视化开发环境 visual python"
- win.close = False
- win.background = "b1.jpg"
- win.show_all()
- gtk.main()
只需要輕鬆的設置.
.close = False .max = False 就关掉不要要的标题东西.
设置为真后,有可以显示出来.
.background 轻松设置背景图.
很像C#,易语言.
有时间后,就将按钮的图片设置也傻瓜化.原状态, 鼠标划过状态, 鼠标按下.
还有一些等等控件.
下面有事件再弄弄那个可视化开发工具.
界面设计器源码.
界面设计器.tar.gz
这个源代码只是我想试试,能否可以实现可视化开发环境.
现在正准备将 这个傻瓜的UI库和可视化合位一体.
凡事不理论啊,实践出点东西才放屁....
:
Reply Like 0 View the author