2015年1月24日 星期六

pygtk圖片,隨著視窗改變大小(更新)


 寫了一個image_viewport類別,繼承Gtk.Image,其實還不熟,沒呼叫super,先不管這有沒有,看重點__init__(self,image, viewport)是建構式,初始化物件,self是自帶,就image,viewport這2個參數,
 其實我只用到3個方法def i_get_viewport_h_size,def i_get_viewport_w_size,def i_image_resize(self,widget,viewport,filepath),不知為什用Anjata在開的副程式file,註解打中文,無法執行,註解只好敲我超爛程度的英文

size_image.py程式

from gi.repository import Gtk, GdkPixbuf, Gdk
import sys,os
from gi.repository.GdkPixbuf import Pixbuf, InterpType
class image_viewport(Gtk.Image):

 def __init__(self,image, viewport):
        #inital data
        self.w_size=0
        self.h_size=0
        self.image = image
        self.viewport = viewport
 def i_set_image(self,image):
       self.image= image
     
 def i_get_image(self,image):
       return self.image
 def i_set_viewport(self,viewport):
       self.viewport= viewport
 def i_get_viewport(self):
       return self.viewport
 def i_get_viewport_h_size(self):
       return self.h_size
 def i_get_viewport_w_size(self):
       return self.w_size     
     
 def i_image_resize(self,widget,viewport,filepath):
    
        self.h_size=viewport.get_allocation().height
        self.w_size=viewport.get_allocation().width
        #put image to buffer
        pixbuf = Pixbuf.new_from_file(filepath)
        #reset size
        pixbuf = pixbuf.scale_simple(self.w_size, self.h_size, InterpType.BILINEAR)
        #set buffer to image
        widget.set_from_pixbuf(pixbuf)

 在主程式,一開始,初始化產生了一個image_viewport實體,叫self.image_viewport_new
        #初始化,產生自定義image_viewport CLASS
        self.image_viewport_new=size_image.image_viewport(self.image,self.viewport)

在主程式,IMAGE_VIEWER是檔案選擇器按鈕,連結信號method

 def IMAGE_VIEWER(self, widget, *event):
        #取得file
        self.file=self.filechooserbutton.get_filename()
        #i_image_resize方法,image跟viewport一樣大小
        self.image_viewport_new.i_image_resize(widget,self.viewport,self.file)


在主程式,IMAGE_RESIZE是window,check-resize連結信號method,注意要判斷,否則信號會變迴圈,一直發出信號
  
    def IMAGE_RESIZE(self, widget, *event):
      
        #目前viewport size
        w_size=self.viewport.get_allocation().width
        h_size=self.viewport.get_allocation().height
        #存在記憶體viewport size
        wv=self.image_viewport_new.i_get_viewport_w_size()
        hv=self.image_viewport_new.i_get_viewport_h_size()

      
        #檢查viewport是否改變,重設size,否則成為無窮迴圈
        if(wv!=w_size or hv!=h_size ):
           print(str(h_size))
           self.image_viewport_new.i_image_resize(widget,self.viewport,self.file)



下圖是GLADE建的window信號check-resize


主程式


#!/usr/bin/env python
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-
#
# main.py
# Copyright (C) 2015 yplin
#
# pygtk-foobar_u1410 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
# (at your option) any later version.
#
# pygtk-foobar_u1410 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 .

from gi.repository import Gtk, GdkPixbuf, Gdk

import os, sys ,size_image

from gi.repository.GdkPixbuf import Pixbuf, InterpType

#Comment the first line and uncomment the second before installing
#or making the tarball (alternatively, use project variables)
UI_FILE = "src/pygtk_foobar_u1410.ui"
#UI_FILE = "/usr/local/share/pygtk_foobar_u1410/ui/pygtk_foobar_u1410.ui"


class GUI:
    def __init__(self):
        #新建了一個xml-gtk
        self.builder = Gtk.Builder()
        #把UI_FILE加到gtk,glade產生的xml
        self.builder.add_from_file(UI_FILE)
        #建立xml信號連結
        self.builder.connect_signals(self)
        #取得xml UI上所有元件
        self.file="/home/yplin/lion.png"
        self.test_w=0
        self.test_h=0
        self.label=self.builder.get_object('label1')
        self.button2=self.builder.get_object('button2')
        self.button1=self.builder.get_object('button1')
        self.button3=self.builder.get_object('button3')
        self.window=self.builder.get_object('window')
        self.fixed=self.builder.get_object('fixed1')
        self.filechooserbutton=self.builder.get_object('filechooserbutton1')
        self.image=self.builder.get_object('image1')
        self.viewport=self.builder.get_object('viewport1')
        self.image.set_from_file(self.file)
        #初始化,產生自定義image_viewport CLASS
        self.image_viewport_new=size_image.image_viewport(self.image,self.viewport)
       
    def on_window_destroy(self, widget, *event):
        Gtk.main_quit()
   
   
       
    def SHOW(self, widget, *event):
       
        #self.label.set_text(self.button2.get_label())
        #get_focus可取得激活對象
        bt_active_label=self.window.get_focus().get_label()
        #widget是指信號處理對象,用GALADE創建UI時,是為label1
        widget.set_text(bt_active_label)
        #print(user_data)
        #widget.hide()
        #除錯技巧,印出
        #print(str(self.window.get_focus().get_label()))
        #self.button1.set_label("JJJJJJ")
        #重設label位置
        #self.fixed.put(self.label, 80, 40)
    def SHOW_IMAGE(self, widget, *event):
        #取得激活對象按鈕名稱
        bt_active_label=self.window.get_focus().get_label()
        #根據按鈕名稱,顯示或隱藏圖片
        if bt_active_label=='隱藏圖片':widget.hide()
        if bt_active_label=='顯示圖片':widget.show()
        if bt_active_label=="離開":Gtk.main_quit()
    def IMAGE_VIEWER(self, widget, *event):
        #取得file
        self.file=self.filechooserbutton.get_filename()
        #i_image_resize方法,image跟viewport一樣大小
        self.image_viewport_new.i_image_resize(widget,self.viewport,self.file)
   
    def IMAGE_RESIZE(self, widget, *event):
       
        #目前viewport size
        w_size=self.viewport.get_allocation().width
        h_size=self.viewport.get_allocation().height
        #存在記憶體viewport size
        wv=self.image_viewport_new.i_get_viewport_w_size()
        hv=self.image_viewport_new.i_get_viewport_h_size()

       
        #檢查viewport是否改變,重設size,否則成為無窮迴圈
        if(wv!=w_size or hv!=h_size ):
           print(str(h_size))
           self.image_viewport_new.i_image_resize(widget,self.viewport,self.file)
       

         

         
       
       
def main():
    app = GUI()
    app.window.show()
    Gtk.main()
   
   
if __name__ == "__main__":
    sys.exit(main())

執行後如下,圖片是跟著視窗大小跑,可拉大,圖片跟著拉大,但拉大後,不能拉小,應該是Pixbuf的BUG,因為我把圖片隱藏,視窗是可拉小的







好久沒寫程式,犯了一些錯,總覺的有點那不好,不對勁,後來想到了,我已給image,viewport初始化了,記憶體已有image,viewport,在給image,viewport參數會蓋掉原先,呼喚太多次,小程式沒差,程式很大,就有差了,好的設計是越簡單,記憶體用越少越好,會給使用者更好的感受.

   #初始化,產生自定義image_viewport CLASS
        self.image_viewport_new=size_image.image_viewport(self.image,self.viewport)


def i_image_resize(self,widget,viewport,filepath):
    
       這樣寫不好,給filepath參數就可,因為參數在給image(widget),wiewport(viewport),多餘了,程式沒效率,應像下面這樣
 def i_image_resize(self,filepath):

改寫後(紅色地方是改寫地方)

size_image.py程式

from gi.repository import Gtk, GdkPixbuf, Gdk
import sys,os
from gi.repository.GdkPixbuf import Pixbuf, InterpType
class image_viewport(Gtk.Image):

 def __init__(self,image, viewport):
        #inital data
        self.w_size=0
        self.h_size=0
        self.image = image
        self.viewport = viewport
 def i_set_image(self,image):
       self.image= image
      
 def i_get_image(self,image):
       return self.image
 def i_set_viewport(self,viewport):
       self.viewport= viewport
 def i_get_viewport(self):
       return self.viewport
 def i_get_viewport_h_size(self):
       return self.h_size
 def i_get_viewport_w_size(self):
       return self.w_size      
      
 def i_image_resize(self,filepath):
   
        self.h_size=self.viewport.get_allocation().height
        self.w_size=self.viewport.get_allocation().width
        #put image to buffer
        pixbuf = Pixbuf.new_from_file(filepath)
        #reset size
        pixbuf = pixbuf.scale_simple(self.w_size, self.h_size, InterpType.BILINEAR)
        #set buffer to image
        self.image.set_from_pixbuf(pixbuf)


主程式:

#!/usr/bin/env python
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-
#
# main.py
# Copyright (C) 2015 yplin
#
# pygtk-foobar_u1410 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
# (at your option) any later version.
#
# pygtk-foobar_u1410 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 .

from gi.repository import Gtk, GdkPixbuf, Gdk

import os, sys ,size_image

from gi.repository.GdkPixbuf import Pixbuf, InterpType

#Comment the first line and uncomment the second before installing
#or making the tarball (alternatively, use project variables)
UI_FILE = "src/pygtk_foobar_u1410.ui"
#UI_FILE = "/usr/local/share/pygtk_foobar_u1410/ui/pygtk_foobar_u1410.ui"


class GUI:
    def __init__(self):
        #新建了一個xml-gtk
        self.builder = Gtk.Builder()
        #把UI_FILE加到gtk,glade產生的xml
        self.builder.add_from_file(UI_FILE)
        #建立xml信號連結
        self.builder.connect_signals(self)
        #取得xml UI上所有元件
        self.file="/home/yplin/lion.png"
        self.test_w=0
        self.test_h=0
        self.label=self.builder.get_object('label1')
        self.button2=self.builder.get_object('button2')
        self.button1=self.builder.get_object('button1')
        self.button3=self.builder.get_object('button3')
        self.window=self.builder.get_object('window')
        self.fixed=self.builder.get_object('fixed1')
        self.filechooserbutton=self.builder.get_object('filechooserbutton1')
        self.image=self.builder.get_object('image1')
        self.viewport=self.builder.get_object('viewport1')
        self.image.set_from_file(self.file)
        #初始化,產生自定義image_viewport CLASS
        self.image_viewport_new=size_image.image_viewport(self.image,self.viewport)
       
    def on_window_destroy(self, widget, *event):
        Gtk.main_quit()
   
   
       
    def SHOW(self, widget, *event):
       
        #self.label.set_text(self.button2.get_label())
        #get_focus可取得激活對象
        bt_active_label=self.window.get_focus().get_label()
        #widget是指信號處理對象,用GALADE創建UI時,是為label1
        widget.set_text(bt_active_label)
        #print(user_data)
        #widget.hide()
        #除錯技巧,印出
        #print(str(self.window.get_focus().get_label()))
        #self.button1.set_label("JJJJJJ")
        #重設label位置
        #self.fixed.put(self.label, 80, 40)
    def SHOW_IMAGE(self, widget, *event):
        #取得激活對象按鈕名稱
        bt_active_label=self.window.get_focus().get_label()
        #根據按鈕名稱,顯示或隱藏圖片
        if bt_active_label=='隱藏圖片':widget.hide()
        if bt_active_label=='顯示圖片':widget.show()
        if bt_active_label=="離開":Gtk.main_quit()
    def IMAGE_VIEWER(self, widget, *event):
        #取得file
        self.file=self.filechooserbutton.get_filename()
        #i_image_resize方法,image跟viewport一樣大小
        self.image_viewport_new.i_image_resize(self.file)
   
    def IMAGE_RESIZE(self, widget, *event):
       
        #目前viewport size
        w_size=self.viewport.get_allocation().width
        h_size=self.viewport.get_allocation().height
        #存在記憶體viewport size
        wv=self.image_viewport_new.i_get_viewport_w_size()
        hv=self.image_viewport_new.i_get_viewport_h_size()

       
        #檢查viewport是否改變,重設size,否則成為無窮迴圈
        if(wv!=w_size or hv!=h_size ):
           print(str(h_size))
           self.image_viewport_new.i_image_resize(self.file)
       

         

         
       
       
def main():
    app = GUI()
    app.window.show()
    Gtk.main()
   
   
if __name__ == "__main__":
    sys.exit(main())


沒有留言: