def IMAGE_VIEWER(self, widget, *event):
file=self.filechooserbutton.get_filename()
#print(str(self.filechooserbutton.get_current_folder_uri()))
#print(str(file))
widget.set_from_file(file)
看一下pygtk手冊filechosserbutton繼承如下,在FileChooser找到取得file名稱的方法get_filename(),
+-- gobject.GObject +-- gtk.Object +-- gtk.Widget +-- gtk.Container +-- gtk.Box +-- gtk.HBox +-- gtk.FileChooserButton (implements gtk.FileChooser)
file=self.filechooserbutton.get_filename()取得file路徑名稱的方法
widget.set_from_file(file)把imgage設成 file,圖片就改變了,widget變數是指信號處理對象(GLADE敲入IMAGE_VIEWER信號時,選的那個信號處理對象)
再來是用GLADE產生檔案過濾器,我只想要jpg或png檔,如下圖1,filechooserbutton的一般條件選filefilter1,動作選開啟
b然後widgets會出現filter1,點它,如下圖2,一般的模式中敲入要過濾條件(*.jpg......),如此,filechooser只會顯示過濾條件的檔案
python程式如下
#!/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
#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.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.image.set_from_file("/home/yplin/lion.png")
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
file=self.filechooserbutton.get_filename()
#print(str(self.filechooserbutton.get_current_folder_uri()))
#print(str(file))
#file加入image
widget.set_from_file(file)
def main():
app = GUI()
app.window.show()
Gtk.main()
if __name__ == "__main__":
sys.exit(main())
執行的樣子
沒有留言:
張貼留言