Search code examples
python-2.7wxpython

wxpython how to set a bind event on a button which gets enable upon clicking on other button


which consist a combo box 4 buttons. Once i select an entry from combo box, it will enable a button upon clicking one button it enables the rest. I want to send a command once the buttons is enabled on clicking it. Below is my code:

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)

I want to send a command once this button self.act_button.Enable(True) gets enabled.


Solution

  • You can bind the button's to events before you disable them. They aren't going to react to events (other than maybe mouse events) until you enable them. There is no reason to bind events when you enable the button.

    If you want to call a function after the enabling process (i.e. self.act_button.Enable(True)), then just call the function right after that:

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

    If you want to create some kind of custom event, then you'll want to look into how to use wx.PostEvent and wx.lib.newevent. The following resources might interest you as well: