How disable wxPython button when something found in db?

Viewed 29

i have some small project in my work, and i cant figure out, how its must be done. so, i build some small wxPython panel and button, and i wanna disable that one button when i found from db multiple files.

Heres my code:

class Settings(wx.Frame):

def __init__(self, parent):
    wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition,
                      size=wx.Size(700, 405), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
    self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)
    frame_sizer = wx.BoxSizer(wx.VERTICAL)
    self.main_panel = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)
    self.main_sizer = wx.BoxSizer(wx.VERTICAL)
    frame_sizer.Add(self.main_panel, 1, wx.EXPAND)
    self.SetSizer(frame_sizer)
    self.save_button = wx.Button(self.main_panel, wx.ID_ANY, u"Save", wx.DefaultPosition, wx.DefaultSize, 0)
    #bottom_buttons_sizer.Add(self.save_button, 0, wx.ALL, 5)
    self.Layout()
    self.Centre(wx.BOTH)

    self.Bind(wx.EVT_BUTTON, self.onDisable)
    self.Bind(wx.EVT_BUTTON, self.anotherFunc)



def SerializationDB(self):
    con = sl.connect('Test.db')
    cursor = con.cursor()
    cursor.execute('CREATE TABLE if not exists TESTS(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name, reference_script)').fetchone()
    with con:
        sql = 'INSERT INTO TESTS (name, reference_script) values(?, ?)'
        data = [('Test 1', 'TestText')]
        p = cursor.executemany(sql, data)
    return p

def checkNames(self, e):
    con = sl.connect('Test.db')
    cursor = con.cursor()
    k = cursor.execute('SELECT name, COUNT(*) from TESTS WHERE id between 1 and 30 group by name HAVING COUNT(*)').fetchone()[1]
    return k

def onDisable(self, e):
    if self.checkNames(e) > 1:
        self.save_button.Disable() #<- doesnt work.


def anotherFunc(self, e):
    self.save_button.Enable()

I dont know how those binds works, for any advice appreciated.

How i can unbind(?) or do something with that button, when another functions was running?

1 Answers

Your code would work, except you have overridden the first Bind with another Bind for the same wx.EVT_BUTTON event, so it is always performing:

 def anotherFunc(self, e):
    self.save_button.Enable()

(as a side note, never underestimate the utility of the print command, for debugging purposes. A simple print("function name") at the top of the function, uncovers these sorts of errors easily)

Here is your code, amended to swap the function that the button performs, if there is already data in the database.
(I've had to cheat and call self.SerializationDB() at the start, to create the database on my machine.

import wx
import sqlite3

class Settings(wx.Frame):

    def __init__(self, parent):
        wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition,
                      size=wx.Size(700, 405), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
        self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)
        frame_sizer = wx.BoxSizer(wx.VERTICAL)
        self.main_panel = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)
        self.main_sizer = wx.BoxSizer(wx.VERTICAL)
        frame_sizer.Add(self.main_panel, 1, wx.EXPAND)
        self.SetSizer(frame_sizer)
        self.save_button = wx.Button(self.main_panel, wx.ID_ANY, u"Save", wx.DefaultPosition, wx.DefaultSize, 0)
        #bottom_buttons_sizer.Add(self.save_button, 0, wx.ALL, 5)
        self.Layout()
        self.Centre(wx.BOTH)

        #self.Bind(wx.EVT_BUTTON, self.onDisable)
        #self.Bind(wx.EVT_BUTTON, self.anotherFunc)

        # binding to a specific button to a specific function allows you to add more buttons
        # performing other function
        self.save_button.Bind(wx.EVT_BUTTON, self.onDisable)

        # for testing purposes I create/update the database here
        self.SerializationDB()

        self.Show()


    def SerializationDB(self):
        con = sqlite3.connect('Test.db')
        cursor = con.cursor()
        cursor.execute('CREATE TABLE if not exists TESTS(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name,       reference_script)').fetchone()
        if self.checkNames() < 1:
            with con:
                sql = 'INSERT INTO TESTS (name, reference_script) values(?, ?)'
                data = [('Test 1', 'TestText')]
                p = cursor.executemany(sql, data)
        else:
            p = 1
        return p

    def checkNames(self):
        con = sqlite3.connect('Test.db')
        cursor = con.cursor()
        k = cursor.execute('SELECT name, COUNT(*) from TESTS WHERE id between 1 and 30 group by name HAVING COUNT(*)').fetchone()
        if k:
            k = k[1]
        else:
            k = 0
        return k

    def onDisable(self, e):
        print("Save Button")
        if self.checkNames() > 0:
            self.save_button.Disable()
            self.save_button.Unbind(wx.EVT_BUTTON)    
            self.save_button.Bind(wx.EVT_BUTTON, self.anotherFunc)
            self.save_button.SetLabel("Another")
            self.save_button.Enable()

    def anotherFunc(self, e):
        print("anotherFunction")
        
if __name__ == "__main__":
    app = wx.App()
    Settings(None)
    app.MainLoop()        
Related