wxpython如何在按钮上设置绑定事件,该按钮在单击其他按钮时启用

时间:2016-11-15 08:17:18

标签: python-2.7 wxpython

它包含一个组合框4个按钮。一旦我从组合框中选择一个条目,它将在单击一个按钮时启用一个按钮,然后启用其余按钮。我想在点击它时启用按钮后发送命令。 以下是我的代码:

import wx
import xlrd
import os,sys,time
folderpath = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
print folderpath
site_lib_path = os.path.join(folderpath, "site_lib")

files = os.listdir(site_lib_path)
for file in files:
    sys.path.append(os.path.join(site_lib_path, file))

from printercx import printercx
from resttestservice.resttestservice import UITest

class ui(wx.Frame):

"""
This Class will create a Sample Frame and Create Two Buttons on tha Panel.
"""

def __init__(self,parent,id):
    """
        This Fucntion will create a Frame and a Panel which has Two buttons: "OK" and "Cancel"

    """
    """-----SALQE Connecttion-----------"""
    self.connection = printercx.deviceConnection()
    self.ui = UITest(self.connection)

    """-----------Window Bar Name------"""
    wx.Frame.__init__(self,parent,id,'GEN-2 Tool',size=(600,500))
    panel=wx.Panel(self)

    """-----------Heading-------"""
    header_text = wx.StaticText(panel, label="GEN-2 Tool", pos=(250,30))
    font = wx.Font(15, wx.DECORATIVE, wx.NORMAL, wx.BOLD)
    header_text.SetFont(font)        
    wx.StaticLine(panel, pos=(10, 75), size=(690,3))

    """-----------Buttons-------"""
    self.pre_button=wx.Button(panel,label="Precondition",pos=(50,250),size=(100,40))
    self.act_button=wx.Button(panel,label="Action",pos=(450,250),size=(100,40))
    self.pass_button=wx.Button(panel,label="Pass",pos=(50,350),size=(100,40))
    self.fail_button=wx.Button(panel,label="Fail",pos=(450,350),size=(100,40))

    """-------------------------------Excel-------------------------------------------------------"""
    self.mainList=[]
    self.val_list=[]
    dic={}
    book=xlrd.open_workbook("Reference_Mapping.xlsx")
    sheet=book.sheet_by_name("TestCases")
    n_row= sheet.nrows-1
    n_col=sheet.ncols
    row=1
    while row<=n_row:
        smallList=[]
        col=0
        while col<n_col:
            cel=sheet.cell(row,0)
            if cel.value!="":
                self.val_list.append(cel.value)
            key=sheet.cell(0,col).value
            val=sheet.cell(row,col).value
            dic[key]=val
            col+=1
        smallList.append(dic.copy())
        self.mainList.append(smallList)
        row+=1
    self.val_list= list(set(self.val_list))
    """-------------------------------------------------------------------------------------------------"""
    """-----------Combo Box with Text-------"""
    text=wx.StaticText(panel, label="Test Case: ", pos=(150,130))
    font = wx.Font(10,wx.DECORATIVE, wx.NORMAL, wx.BOLD)
    text.SetFont(font)
    self.val_list.insert(0, "Select")
    self.combobox=wx.ComboBox(panel, value=self.val_list[0], pos=(300,130), choices=self.val_list,style=wx.CB_READONLY)
    self.Bind(wx.EVT_COMBOBOX, self.onTestCaseSelection, self.combobox)
    print "-----------"
def onTestCaseSelection(self,event):
    if self.combobox.GetSelection()>0:
        print self.combobox.GetValue()
        """---------- Compairing Key's values--------------"""
        for each in range(len(self.mainList)):
            for every in range(len(self.mainList[each])):
                if self.mainList[each][every]['TC_ID']==self.combobox.GetValue():
                    if self.mainList[each][every]['Ref_ID_Pre']=="":
                        if self.mainList[each][every]['Ref_ID_Post']!="":
                            self.pre_button.Enable(False)
                            self.act_button.Enable(True)
                            self.Bind(wx.EVT_BUTTON,self.send_udw,self.act_button)
                            self.pass_button.Enable(True)
                            self.fail_button.Enable(True)

                    if self.mainList[each][every]['Ref_ID_Pre']!="":
                        if self.mainList[each][every]['Ref_ID_Post']=="":
                            self.pre_button.Enable(True)
                            self.Bind(wx.EVT_BUTTON,self.send_udw,self.pre_button)
                            self.act_button.Enable(False)
                            self.pass_button.Enable(False)
                            self.fail_button.Enable(False)

                    if self.mainList[each][every]['Ref_ID_Pre']!="":
                        if self.mainList[each][every]['Ref_ID_Post']!="":
                            action_button_cmd=self.mainList[each][every]['Ref_ID_Post']
                            self.pre_button.Enable(True)
                            self.Bind(wx.EVT_BUTTON,self.send_udw,self.pre_button)
                            self.act_button.Enable(False)
                            self.pass_button.Enable(False)
                            self.fail_button.Enable(False)                                
    else:
        self.disableAllControls(without=None)

def disableAllControls(self, without=None):
    if  without==None:
        self.pre_button.Enable(False)
        self.act_button.Enable(False) 
        self.pass_button.Enable(False)
        self.fail_button.Enable(False)

def send_udw(self,event):

    for each in range(len(self.mainList)):
        for every in range(len(self.mainList[each])):

            if self.mainList[each][every]['TC_ID']==self.combobox.GetValue():
                if self.mainList[each][every]['Ref_ID_Pre']=="":
                    if self.mainList[each][every]['Ref_ID_Post']!="":
                        if self.act_button.IsEnabled()==True:
                            post_command=self.mainList[each][every]['Ref_ID_Post']
                            post_udw="ui_v3.move_to_state "+post_command+" 1"
                            self.connection.udw(post_udw)

                if self.mainList[each][every]['Ref_ID_Pre']!="":
                    if self.mainList[each][every]['Ref_ID_Post']=="":
                        if self.pre_button.IsEnabled()==True:
                            post_command=self.mainList[each][every]['Ref_ID_Pre']
                            post_udw="ui_v3.move_to_state "+post_command+" 1"
                            self.connection.udw(post_udw)

                if self.mainList[each][every]['Ref_ID_Pre']!="":
                    if self.mainList[each][every]['Ref_ID_Post']!="":
                        if self.pre_button.IsEnabled()==True:
                            post_command=self.mainList[each][every]['Ref_ID_Pre']
                            post_udw="ui_v3.move_to_state "+post_command+" 1"
                            print post_udw
                            self.connection.udw(post_udw)
                            """----Enabling button---"""
                            self.pre_button.Enable(False)
                            self.act_button.Enable(True)
                            self.pass_button.Enable(True)
                            self.fail_button.Enable(True)
                            time.sleep(1)

我想在启用此按钮self.act_button.Enable(True)后发送命令。

1 个答案:

答案 0 :(得分:1)

您可以在禁用按钮之前将按钮绑定到事件。在启用它们之前,它们不会对事件(除了鼠标事件之外)做出反应。启用按钮时没有理由绑定事件。

如果你想在启用过程之后调用一个函数(即self.act_button.Enable(True)),那么只需在之后调用该函数:

self.act_button.Enable(True) 
self.myFunction(*args, **kwargs)

如果您想创建某种自定义事件,那么您需要了解如何使用wx.PostEventwx.lib.newevent。您也可能会对以下资源感兴趣: