組み込み技術を向上したい!!

でも組み込み以外にも手を出し始めました・・・

kivy+popup

kivyでは簡単にpopupを作れるんだが、popup内でyes/noのボタンを作るのにちょっとなやんだのでメモ。

from kivy.uix.popup import Popup

でインポートして、公式サイト通りにやれば、

popup = Popup(title='Test popup',
    content=Label(text='Hello world'),
    size_hint=(None, None), size=(400, 400))

でポップアップは簡単に作れて、popup.open()で表示させればいいだけなんだけど、これだとLabel一つしかcontentに含まれない。

いろいろ思考錯誤した結果、たぶんこれで正解だろうとたどり着いたのはcontentにFloatLayout()とかのオブジェクト?を入れてやって、add_widgetでButtonやLabelを追加してやることだった。つまり、

   self.PopContent = FloatLayout()  
        self.PopBtn_yes = Button(text=u'yes', font_size = "18sp", size_hint=(.3, .3), pos_hint={'center_x': .3, 'center_y': .4})            
        self.PopContent.add_widget(self.PopBtn_yes)
        self.PopBtn_no = Button(text=u'no', font_size = "18sp", size_hint=(.3, .3), pos_hint={'center_x': .7, 'center_y': .4})
        self.PopContent.add_widget(self.PopBtn_no)
        self.PopLbl = Label(text=u'yes or no', font_size = "20sp", size_hint=(.3, .2), pos_hint={'center_x': .5, 'center_y': .9})
        self.PopContent.add_widget(self.PopLbl)
        self.Popup = Popup(title=u'pop button test',title_color=(1,0.2,0.2,1),title_size="20sp",content=self.PopContent, size_hint=(None, None),size=(600,300), auto_dismiss=False)
        self.Popup.bind(on_dismiss=self.PopCallback)
        self.PopBtn_yes.bind(on_press=self.PopYesCallback)
        self.PopBtn_no.bind(on_press=self.PopNoCallback)
        

みたいにするとイメージした感じでできた。

auto_dismiss=Falseとしているので、PopYesCallbackやPopNoCallbackの処理でself.Popup.dismiss()的な処理をしてdismissする必要がある。

ちゃんと本とか読んでpython勉強している人にはサクッとわかるんだろうなぁ・・・。本は苦手で・・・。