2019年1月19日土曜日

wxPython ツールバーからの各ボタン(ラベル)のイベントの取得





詳細

以下のようにツールバーtoolbarを作成して、btに対してbindすればいいのです。
bt = self.toolbar.AddLabelTool(ID, fName, bm)
self.Bind(wx.EVT_TOOL, self.OnTool, bt)

しかし、これだとたくさんあるツールバーのボタンに対して、面倒だし
ボタンが思いがけずに増えた場合など、煩雑になります。


対応策

(画像付きボタンを作成しています)
例えば
①toolフォルダに各ボタンに付ける画像をいれます。
②以下のコードで画像が適当にリサイズされ、適当なIDでツールバーtoolbar
  に加えられます。

    #ツールボタン
    fpath = "./tool/*."
    syurui= ["png", "jpg", "gif" , "bmp", "jpeg"]

    for s in syurui:
        fpath_all = fpath + s
        #print "fpath_all",fpath_all
        files = glob.glob(fpath_all)
        print "tuultuul  files",files
        for f in files:
            #ファイルパスから拡張子を除いたファイル名を取得
            base = os.path.basename(f)
            fName = os.path.splitext(base)[0]
            #self.toolNameList.append(fName)
            #print "fName",fName 
         
            image = wx.Image(f)
            image.Rescale(15,15)
            bm = image.ConvertToBitmap()
            ID = wx.NewId()
            bt = self.toolbar.AddLabelTool(ID, fName, bm)
            self.toolBtList.append(bt)
            self.toolIdList.append(ID)


③ツールボタン全てのイベントをOnToolで受けます。
        for tbt in self.toolBtList:
            self.Bind(wx.EVT_TOOL, self.OnTool, tbt)


④event.Id で OnToolで各ボタンごとのイベントに分岐します。
    def OnTool(self, event):
        print "OnTool"
        for id in self.toolIdList:
            if event.Id == id:
                print "event.Id",event.Id



失敗したこと

OnToolの各ボタンのイベントの分岐を、以下の関数でしようとしてもでき 
ませんでした。
 self.toolbar.FindById(id) 
 ツールバー自体のイベントを取得しているような。 全てTrueになる?
 self.FindWindowById(id)  全てFalse?

 event.GetEventObject()
 event.GetSelection()
 GetID()
 self.toolbar.FindById(Id):


全コード

E:\MyBackups\goolgedrive\myprg_main\python_my_prg\wxpython\dc\お絵かき4\all2_mytoolbar.py

# coding: UTF-8

import os
import glob
import wx


"""ダブルバッファで表示するFrame"""
class DrawPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)


def myToolBar(parent):
    parent.toolbar = parent.CreateToolBar(wx.TB_TEXT)
    #toolbar = self.CreateToolBar(wx.TB_TEXT)

    #以下のように画像ファイルを読み込んで SetSizeでサイズを変更しようと
    #したら、切り取られた形でサイズが変更された。Resize か Rescaleのコマ
    #ンドが Bitmapにはない。
    # で Image を使って画像表示 Rescaleをする。
    #bm1 = wx.Bitmap('./move.jpg')
    #bm1.SetSize((20,20))

    ##bm1_image = "move.jpg"
    #bm1_image = "drop.png"
    #image1 = wx.Image(bm1_image)
    #image1.Rescale(15,15)
    #bm1 = image1.ConvertToBitmap()
    #toolbar.AddLabelTool(wx.ID_ANY, 'drop', bm1)

    #コントロールボタン
    #フォルダから画像を読み込み、画像付きボタンにしている
    #それをツールバーに加えている
    fpath = "./ctr/*."
    syurui= ["png", "jpg", "gif" , "bmp", "jpeg"]

    for s in syurui:
        fpath_all = fpath + s
        print "fpath_all",fpath_all
        files = glob.glob(fpath_all)
        print "files",files
        for f in files:
            #ファイルパスから拡張子を除いたファイル名を取得
            base = os.path.basename(f)
            fName = os.path.splitext(base)[0]
            #parent.toolNameList.append(fName)
            print "fName",fName 
         
            image = wx.Image(f)
            image.Rescale(15,15)
            bm = image.ConvertToBitmap()
            ID = wx.NewId()
            bt = parent.toolbar.AddLabelTool(ID, fName, bm)
            parent.ctrBtList.append(bt)
            parent.ctrIdList.append(ID)

            ##wxPythonには予め画像が用意されている
            #parent.dropBt = parent.toolbar.AddLabelTool(wx.ID_ANY, 'drop', wx.ArtProvider.GetBitmap(wx.ART_REDO))


    #ツールボタン
    fpath = "./tool/*."
    syurui= ["png", "jpg", "gif" , "bmp", "jpeg"]

    for s in syurui:
        fpath_all = fpath + s
        #print "fpath_all",fpath_all
        files = glob.glob(fpath_all)
        print "tuultuul  files",files
        for f in files:
            #ファイルパスから拡張子を除いたファイル名を取得
            base = os.path.basename(f)
            fName = os.path.splitext(base)[0]
            #parent.toolNameList.append(fName)
            #print "fName",fName 
         
            image = wx.Image(f)
            image.Rescale(15,15)
            bm = image.ConvertToBitmap()
            ID = wx.NewId()
            bt = parent.toolbar.AddLabelTool(ID, fName, bm)
            parent.toolBtList.append(bt)
            parent.toolIdList.append(ID)


    #画像ボタン
    fpath = "./gazou/*."
    syurui= ["png", "jpg", "gif" , "bmp", "jpeg"]
    for s in syurui:
        fpath = fpath + s
        files = glob.glob(fpath)
        for f in files:
            #ファイルパスから拡張子を除いたファイル名を取得
            base = os.path.basename(f)
            fName = os.path.splitext(base)[0]

            image = wx.Image(f)
            image.Rescale(15,15)
            bm = image.ConvertToBitmap()
            ID = wx.NewId()
            #print "ID",ID
            bt = parent.toolbar.AddLabelTool(ID, fName, bm)
            #bt = parent.toolbar.AddLabelTool(wx.ID_ANY, fName, bm)
            #parent.gazouBtList.append(bt)
            parent.gazouBtList.append(bt)
            parent.gazouIdList.append(ID)

    parent.toolbar.Realize()


class MyFrame(wx.Frame):
    #メニューバー 、ステータスバー,ツールバーなどはFrmae上し
    #か置けないため、paelを作成し、そこで描画する
    ## *args 可変長タプル  **kwargs 可変長辞書
    def __init__(self, parent, *args ,**kargs):
        frame = wx.Frame.__init__(self, parent, *args, **kargs)
        #イベントオブジェクトの分類
        self.gazouBtList = []
        self.gazouIdList = []
        self.toolBtList  = []
        self.toolIdList  = []
        self.ctrBtList  = []
        self.ctrIdList   = []

        self.toolbar = None
        myToolBar(self) 

        pal = DrawPanel(self)

        for cbt in self.ctrBtList:
            self.Bind(wx.EVT_TOOL, self.OnCtr, cbt)
        for gbt in self.gazouBtList:
            self.Bind(wx.EVT_TOOL, self.OnGazou, gbt)
        for tbt in self.toolBtList:
            self.Bind(wx.EVT_TOOL, self.OnTool, tbt)

        self.Show()


    def OnCtr(self, event):
        print "OnCtr"
        for id in self.ctrIdList:
            if event.Id == id:
                print "event.Id",event.Id

    def OnGazou(self, event):
        print "ongazou"
        for id in self.gazouIdList:
            if event.Id == id:
                print "event.Id",event.Id

    def OnTool(self, event):
        print "OnTool"
        for id in self.toolIdList:
            if event.Id == id:
                print "event.Id",event.Id

    #例えば、ツールボタンを押しても OnToolでイベント処理させて
    #以下のように各ボタンのイベントの分岐を、以下の関数でしようとしてもでき 
    #なかった。ツールバーのボタン(ラベル?)ごとのイベントを受け取るには
    # event.Id  でよかった。

    #def OnTool(self, event):
    #    if self.toolbar.FindById(id):
    #
    #    if self.FindWindowById(id):
    #     
    #    tbt = event.GetEventObject()
    #
    #    tbt = event.GetSelection()
    #
    #    id = GetID()
    #
    #    if  self.toolbar.FindById(Id):


if __name__ == '__main__':
    app = wx.App(False)
    MyFrame(None, -1, "Draw Test 4", size=(800, 600))
    app.MainLoop()


0 件のコメント:

コメントを投稿

About

参加ユーザー

連絡フォーム

名前

メール *

メッセージ *

ブログ アーカイブ

ページ

Featured Posts