0

ボタンを押したときにボタン ラベルを Excel に印刷したいのですが、Excel ファイルが作成されましたが、情報を送信できません。

import wx
from xlwt import *

w = Workbook()
ws1 = w.add_sheet('sheet 1')

class MyFrame(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Button to Excel', size = (300,300))
        panel=wx.Panel(self)

        extBtn = wx.Button(panel, label="Exit",pos=(100,150))
        extBtn.Bind(wx.EVT_BUTTON, self.onClose)

        btn = wx.Button(panel,label = "Mem 1",pos=(100,100))
        btn.Bind =(wx.EVT_BUTTON,self.onButton)

    def onClose(self, event):
        self.Close()

    def onButton(self,event):
        print self.GetLabel() in ws1

if __name__ == '__main__':
    app=wx.PySimpleApp()
    frame=MyFrame(parent=None,id=-1)
    frame.Show()
    app.MainLoop()
    w.save('a.xls')
4

1 に答える 1

1

いくつかの問題があります。まず、2 番目のボタンを正しくバインドしません。最初のものと同じ方法でバインドする必要があります。したがって、バインディング コードを次のように変更します。

btn.Bind(wx.EVT_BUTTON, self.onButton)

等号がなくなったことに注意してください。

次にonButtonメソッドで、データを Excel ファイルに書き込む必要があります。Python の "in" 演算子はそれを行いません。アイテムがコレクションに含まれているかどうかをテストするために使用されます。詳細については、ドキュメントを参照してください。

代わりに、xlwt のwriteメソッドを使用してラベルをセルに書き込みます。完全な例を次に示します。

import wx
from xlwt import *

w = Workbook()
ws1 = w.add_sheet('sheet 1')

class MyFrame(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Button to Excel', size = (300,300))
        panel=wx.Panel(self)

        extBtn = wx.Button(panel, label="Exit",pos=(100,150))
        extBtn.Bind(wx.EVT_BUTTON, self.onClose)

        btn = wx.Button(panel,label = "Mem 1",pos=(100,100))
        btn.Bind(wx.EVT_BUTTON, self.onButton)

    def onClose(self, event):
        w.save('a.xls')
        self.Close()

    def onButton(self,event):
        btn = event.GetEventObject()
        lbl = btn.GetLabel()
        ws1.write(0, 0, lbl)


if __name__ == '__main__':
    app=wx.PySimpleApp()
    frame=MyFrame(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

また、保存を onClose 関数に移動したことに注意してください。

于 2013-10-08T13:49:25.700 に答える