1

wxPython を使い始めたばかりで、バインディングに問題があります。

通常、ボタンイベントをバインドするために私が見つけた例は、self.Bind(wx.EVT_BUTTON, self.OnWhatEverFunction, button). 内部にパネルとボタンを備えたフレームがあり、何を試しても、結果は常に一瞬で表示される点滅フレームであり、それだけです。コードはそれほど多くないので、ここに添付します。あなたの 1 人が私の小さな問題から抜け出す方法を教えてくれることを願っています。

前もって感謝します、トーマス

#!/usr/bin/python
import wx

class CPFSFrame(wx.Frame):
    def __init__(self, parent, title):
        super(CPFSFrame, self).__init__(parent, title=title,
                                    style = wx.BORDER | wx.CAPTION | wx.SYSTEM_MENU | wx.CLOSE_BOX,
                                    size=(350, 200))
        panel = wx.Panel(self, -1)

        pNumberLabel = wx.StaticText(panel, -1, 'Project number: ')
        pNumberText = wx.TextCtrl(panel, -1, '', size=(175, -1))
        pNumberText.SetInsertionPoint(0)

        pNameLabel = wx.StaticText(panel, -1, 'Project name: ')
        pNameText = wx.TextCtrl(panel, -1, '', size=(175, -1))
        pNameText.SetInsertionPoint(0)

        pButton = wx.Button(panel, label='Create')
        pButton.Bind(wx.EVT_BUTTON, self.OnCreate)

        sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6)
        sizer.AddMany([pNumberLabel, pNumberText, pNameLabel, pNameText, pButton])
        panel.SetSizer(sizer)

        statusBar = self.CreateStatusBar()

    def OnCreate(self, evt):
        self.Close(True)

        self.Centre()
        self.Show()

if __name__ == '__main__':
    app = wx.App()
    CPFSFrame(None, title='Create New Project Folder Structure')
    app.MainLoop()
4

1 に答える 1

0

コードのinitセクションに「self.Show()」メソッドを配置する必要があります。最初は OnCreate() が実行されていると思っていましたが、実行されている場合はすぐにフレームを閉じるだけなので、何も表示されません。代わりに OnClose と呼びます。これが実際の例です:

import wx

class CPFSFrame(wx.Frame):
    def __init__(self, parent, title):
        super(CPFSFrame, self).__init__(parent, title=title,
                                    style = wx.BORDER | wx.CAPTION | wx.SYSTEM_MENU | wx.CLOSE_BOX,
                                    size=(350, 200))
        panel = wx.Panel(self, -1)

        pNumberLabel = wx.StaticText(panel, -1, 'Project number: ')
        pNumberText = wx.TextCtrl(panel, -1, '', size=(175, -1))
        pNumberText.SetInsertionPoint(0)

        pNameLabel = wx.StaticText(panel, -1, 'Project name: ')
        pNameText = wx.TextCtrl(panel, -1, '', size=(175, -1))
        pNameText.SetInsertionPoint(0)

        pButton = wx.Button(panel, label='Create')
        pButton.Bind(wx.EVT_BUTTON, self.OnClose)

        sizer = wx.FlexGridSizer(cols=2, hgap=6, vgap=6)
        sizer.AddMany([pNumberLabel, pNumberText, pNameLabel, pNameText, pButton])
        panel.SetSizer(sizer)

        statusBar = self.CreateStatusBar()
        self.Show()

    def OnClose(self, evt):
        self.Close(True)

if __name__ == '__main__':
    app = wx.App()
    CPFSFrame(None, title='Create New Project Folder Structure')
    app.MainLoop()
于 2013-02-07T14:22:36.207 に答える