如何将python脚本添加到GUI?

时间:2016-03-04 20:59:11

标签: python user-interface wxpython

我在向GUI添加脚本时遇到问题。 Python脚本(运行抓取并比较刮除日期)和GUI工作正常。我不确定如何做到这一点,我只是将代码复制到“def run”但后来我得到了错误:“valueerror:signal只能在主线程中工作”。有人可以解释我该怎么做?感谢。

import wx
import wx.lib.mixins.listctrl as listmix
from threading import *

musicdata = {
    0 : ("1", "The Price Of Love", "Rock"),
    1 : ("2", "Tom's Diner", "Rock"),
    2 : ("4", "Praying For Time", "Rock"),
    3 : ("3", "Here We Are", "Rock"),
    4 : ("Linda Ronstadt", "Don't Know Much", "Rock"),
    5 : ("Michael Bolton", "How Am I Supposed To Live Without You", "Blues"),
    6 : ("Paul Young", "Oh Girl", "Rock"),
    }


class WorkerThread(Thread):

    def __init__(self, event):
        Thread.__init__(self)       
        self._want_abort = 0       
        self.start()

    def run(self):
        while not self._want_abort:

            #Here should I place code from script?

            if self._want_abort:
                     return


    def abort(self):
        self._want_abort = 1
########################################################################
class TestListCtrl(wx.ListCtrl):

    #----------------------------------------------------------------------
    def __init__(self, parent, ID=wx.ID_ANY, pos=wx.DefaultPosition,
                 size=wx.DefaultSize, style=0):
        wx.ListCtrl.__init__(self, parent, ID, pos, size, style)


########################################################################
class TestListCtrlPanel(wx.Panel, listmix.ColumnSorterMixin):

    #----------------------------------------------------------------------
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1, style=wx.WANTS_CHARS)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer1 = wx.BoxSizer(wx.HORIZONTAL)

        self.list_ctrl = TestListCtrl(self, size=(-1,100),
                         style=wx.LC_REPORT
                         |wx.BORDER_SUNKEN
                         |wx.LC_SORT_ASCENDING
                         )
        self.list_ctrl.InsertColumn(0, "%")
        self.list_ctrl.InsertColumn(1, "Bookmakers")
        self.list_ctrl.InsertColumn(2, "Date", wx.LIST_FORMAT_RIGHT)
        self.list_ctrl.InsertColumn(3, "League")
        self.list_ctrl.InsertColumn(4, "Teams")
        self.list_ctrl.InsertColumn(5, "1")
        self.list_ctrl.InsertColumn(6, "X")        
        self.list_ctrl.InsertColumn(7, "2")
        self.list_ctrl.InsertColumn(8, "Link") 
        items = musicdata.items()
        index = 0
        for key, data in items:
            self.list_ctrl.InsertStringItem(index, data[0])
            self.list_ctrl.SetStringItem(index, 1, data[1])
            self.list_ctrl.SetStringItem(index, 2, data[2])
            self.list_ctrl.SetStringItem(index, 3, data[2])
            self.list_ctrl.SetItemData(index, key)
            index += 1

        # Now that the list exists we can init the other base class,
        # see wx/lib/mixins/listctrl.py
        self.itemDataMap = musicdata
        listmix.ColumnSorterMixin.__init__(self, 3)
        self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick, self.list_ctrl)


        sizer.Add(sizer1, .1, wx.ALL|wx.EXPAND, 5)
        self.SetSizer(sizer)

        button1 = wx.Button(self,-1,"START")
        self.Bind( wx.EVT_BUTTON,self.button1,button1)
        sizer1.Add(button1,0,0,0)

        button2 = wx.Button(self,-1,"STOP")
        self.Bind( wx.EVT_BUTTON,self.button2,button2)
        sizer1.Add(button2,0,0,0)

        sizer2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer2.Add(self.list_ctrl,1,wx.EXPAND)
        sizer.Add(sizer2,1,wx.EXPAND)


    def button1(self,event):
        self.worker = None
        if not self.worker:
            self.worker = WorkerThread(self)

    def button2(self, event):
        if self.worker:
          self.worker.abort() 


    #----------------------------------------------------------------------
    # Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py
    def GetListCtrl(self):
        return self.list_ctrl

    #----------------------------------------------------------------------
    def OnColClick(self, event):
        print "column clicked"
        event.Skip()

########################################################################
class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Test Program", size=(1000,600))

        a = TestListCtrlPanel(self)

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

0 个答案:

没有答案