2020年3月16日月曜日

wxpython linuxのカラーピッカーは色編集ボタンがないので、色編集が出来るボタンを つくってみた。









wxpython linuxのカラーピッカーは色編集ボタンがないので、色編集が出来るボタンを
つくってみた。


wxpython/お絵かき5/colour_picker.py


  1 # coding: UTF-8
  2
  3 #@@@色設定クラス
  4 #@@@ボタンにより色を変える
  5 #@@@linuxのカラーピッカーには色編集のボタンがない
  6
  7 import wx
  8
  9 class MyFrame(wx.Frame):
 10     def __init__(self):
 11         wx.Frame.__init__(self, None, -1, size = (400, 300))
 12         MyPanel(self)
 13         self.Show()
 14
 15 class MyPanel(wx.Panel):
 16     def __init__(self, parent):
 17         wx.Panel.__init__(self, parent, -1)
 18         JobPalColor(self)
 19
 20 class JobPalColor():
 21     def __init__(self, parent):
 22         self.BtList = []
 23         self.IdList = []
 24         self.FnList = []
 25         self.parent     = parent
 26
 27         self.set_button()
 28         self.color_bt_on_flag = False
 29         self.color_bt_index = 0
 30
 31
 32     #色設定の複数ボタンを作成
 33     def set_button(self):
 34         print "set_button"
 35         gs = wx.GridSizer(8, 3)
 36
 37         #前景色、背景色、表示用ボタン、編集用ボタン
 38         ID = wx.NewId()
 39         frontcBt = wx.Button(self.parent, ID, u"前景色")
 40         frontcBt.SetBackgroundColour("black")
 41         gs.Add(frontcBt, flag=wx.EXPAND|wx.ALL)
 42         self.BtList.append(frontcBt)
 43         self.IdList.append(ID)
 44         self.FnList.append("frontc")
 45
 46         ID = wx.NewId()
 47         backgcBt = wx.Button(self.parent, ID, u"背景色")
 48         backgcBt.SetBackgroundColour("white")
 49         gs.Add(backgcBt, flag=wx.EXPAND|wx.ALL)
 50         self.BtList.append(backgcBt)
 51         self.IdList.append(ID)
 52         self.FnList.append("backgc")
 53
 54         ID = wx.NewId()
 55         hennsyuuBt = wx.Button(self.parent, ID, u"色登録")
 56         gs.Add(hennsyuuBt, flag=wx.EXPAND|wx.ALL)
 57         self.BtList.append(hennsyuuBt)
 58         self.IdList.append(ID)
 59         self.FnList.append("touroku")
 60
 61         #linux版のカラーピッカーには色編集がないため色編集用のボタンを
 62         #21個作成 デフォルトで色をつけてある
 63         color_list = [wx.Colour(0, 255, 255), wx.Colour(255, 0, 255),
 64             wx.Colour(0, 255, 0), wx.Colour(128, 128, 0),
 65             wx.Colour(192, 192, 192),  wx.Colour(0, 128, 128),
 66              "black", "dark olive green", "green",
 67              "maroon", "red",  "yellow", "aquamarine", "blue",
 68              "dark slate blue" , "gray", "lime green", "magenta",
 69              "navy", "purple", "white" ]
 70         for c in color_list:
 71             ID = wx.NewId()
 72             bt = wx.Button(self.parent, ID)
 73             bt.SetBackgroundColour(c)
 74             self.BtList.append(bt)
 75             self.IdList.append(ID)
 76             gs.Add(bt, flag=wx.EXPAND|wx.ALL)
 77
 78         self.parent.SetSizer(gs)
 79
 80         #ボタンのイベントは全てget_jobindexで受ける
 81         for bt in self.BtList:
 82            bt.Bind(wx.EVT_BUTTON, self.get_jobindex)
 83
 84
 85     def get_jobindex(self, event):
 86         print " get_jobindex"
 87         #ボタンのidでイベントを決める事が出来る
 88         index = 0
 89         for id in self.IdList:
 90             #print "id",id
 91             if event.Id == id:
 92                 index = self.IdList.index(id)
 93                 print "index",index
 94                 #以下条件でカラーピッカーを表示
 95                 if (index < 2 and self.color_bt_on_flag == False) or (
 96                         index == 2 and self.color_bt_on_flag == True):
 97                     self.getcolor()
 98
 99         #背景色 前景色でfrontcBt,backgcBtのボタンの色を変える
100         ##frontcBtを押した
101         if index == 0:
102             self.BtList[0].SetBackgroundColour(self.color)
103             self.parent.SetForegroundColour(self.color)
104             self.color_bt_on_flag = False
105
106         ##backgcBtを押した
107         elif index == 1:
108             self.BtList[1].SetBackgroundColour(self.color)
109             self.parent.SetBackgroundColour(self.color)
110             self.color_bt_on_flag = False
111
112         #tourokuBtを押した
113         #押した色ボタンの色を変える
114         elif index == 2:
115             if self.color_bt_on_flag == False:
116                 pass
117             else:
118                 self.BtList[self.color_bt_index].SetBackgroundColour(
119                                                             self.color)
120                 self.color_bt_on_flag = False
121
122         #色ボタンを押した
123         #色ボタン+前景ボタン or 色ボタン+背景ボタン で色を変える
124         else:
125             self.color_bt_index = index
126             self.color_bt_on_flag = True
127             #色ボタンの色に背景色 前景色がセットする事ができる
128             self.color = self.BtList[index].GetBackgroundColour()
129
130
131     #カラーピッカーで色選択 選択した色を返す
132     def getcolor(self):
133         #widowsのようなスタイルのものがでない。色編集がない
134         #ColourPickerCtrl カラーピッカーの単独起動を以下で行っている
135         #ピッカーで塗りつぶりの色を選択
136         d = wx.ColourDialog(self.parent)
137         d.ShowModal()
138         self.color = d.GetColourData().GetColour()
139         d.Destroy()
140
141         self.parent.SetBackgroundColour(self.color)
142         return self.color
143
144
145 if __name__ == '__main__':
146     app = wx.App()
147     MyFrame()
148     app.MainLoop()
                          

0 件のコメント:

コメントを投稿

About

参加ユーザー

連絡フォーム

名前

メール *

メッセージ *

ブログ アーカイブ

ページ

Featured Posts