2 buttons to run 2 different functions? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: 2 buttons to run 2 different functions? (/thread-15225.html) |
2 buttons to run 2 different functions? - JP_ROMANO - Jan-08-2019 Good day all, It seems like the cleanest, easiest way for me to build a GUI is with PySimpleGui, and it's well underway, but I'm having some trouble getting mutliple buttons do run different functions. Hoping it's just a syntax problem, but thought I'd see if anybody here had a few minutes to help. In a nutshell, I want to have 2 buttons A) Check DB for SUID (which will run function 'checkdb()' B) Submit (which will run function 'updatedb()' I've cut out the middle portion of the code, and I know what's IN the functions needs some additional work, but here's what I have at the moment. I noted the two calls with *** since I can't get the bold to work here. I *think* the "checkdb" call at the bottom will execute no matter what, but, as noted, I would like it tied to the SUBMIT button. Thank you for any guidance! import PySimpleGUI as sg import pyodbc import os def checkdb(): connstring = 'DRIVER={SQL Server};SERVER=myserver;DATABASE=mydatabase;UID=myuserid;PWD=mypassword' mysuid = "'" + values[0] +"'" SQLstr="select count(distinct(SUID)) from [DCollection].[dbo].[Contribs] where SUID =" + mysuid print(SQLstr) conn=pyodbc.connect(connstring) cursor=conn.cursor() cursor.execute(SQLstr) result = cursor.fetchone() sg.Popup('Instances in DB:', result[0]) def updatedb(): connstring = 'DRIVER={SQL Server};SERVER=myserver;DATABASE=mydatabase;UID=myuserid;PWD=mypassword' #mysuid = "'" + values[0] +"'" SQLstr="insert into [DCollection].[dbo].[Contribs] SUID =" + mysuid print(SQLstr) conn=pyodbc.connect(connstring) cursor=conn.cursor() cursor.execute(SQLstr) result = cursor.fetchall() for row in result: print (row[0]) sg.ChangeLookAndFeel('BlueMono') layout = [ [], [sg.Text('Please enter the appropriate judgements')], [sg.Text('SUID', size=(10, 1)), sg.InputText(size =(20,1)), sg.InputCombo(('Select Wire Code','19', '42', '51','59', '119', '168','178', '178', '193','327','934','2575','3459'), size=(20, 1)), sg.InputCombo(('Select Contributor','AGEIS', 'BANK OF AMERICA', 'BARCLAYS','CITIBANK', 'CREDIT SUISSE', 'DEUTSCHE BANK','EVERCORE ISI', 'GOLDMAN SACHS', 'GOLDMAN SACHS RD1','JP MORGAN','NOMURA','UBS','WELLS FARGO'), size=(30, 1))], **** [sg.Button('Check DB for SUID')], **** [sg.Text('NI Code 1', size=(10, 1)), sg.InputText(size=(20,1)), sg.InputCombo(('Select Category','ACTION', 'ANALYST', 'INDUSTRY','RATING', 'SETFOCUS', 'TICKER'), size=(20, 1)), sg.InputCombo(('Select Judgement','CORRECTLY ASSIGNED', 'CORRECTLY DERIVED','INCORRECTLY ASSIGNED','INCORRECTLY DERIVED','NOT ASSIGNED','NOT DERIVED','BIO-NO PPLM','NO BIO ON TERMINAL'), size=(30, 1)),sg.Text('NOTES', size=(6, 1)), sg.InputText(size =(60,1))], [sg.Text('NI Code 2', size=(10, 1)), sg.InputText(size=(20,1)), sg.InputCombo(('Select Category','ACTION', 'ANALYST', 'INDUSTRY','RATING', 'SETFOCUS', 'TICKER'), size=(20, 1)), sg.InputCombo(('Select Judgement','CORRECTLY ASSIGNED', 'CORRECTLY DERIVED','INCORRECTLY ASSIGNED','INCORRECTLY DERIVED','NOT ASSIGNED','NOT DERIVED','BIO-NO PPLM','NO BIO ON TERMINAL'), size=(30, 1)),sg.Text('NOTES', size=(6, 1)), sg.InputText(size =(60,1))], [sg.Text('USER: ' + os.getlogin(),font='Helvetica 12 bold')], [sg.Submit(), sg.Cancel()] ] window = sg.Window('Contributor Analysis Input').Layout(layout) event, values = window.Read() *** checkdb()*** window.Close() RE: 2 buttons to run 2 different functions? - JP_ROMANO - Jan-08-2019 I think I figured it out. Will post here for anybody else who may stumble upon this. The way to get the two buttons to do 2 different function calls is to read the text of those buttons - in my case, they are Check DB for SUID, Submit, and Cancel. while True: # Read the Window event, value = window.Read() # Take appropriate action based on button if event == 'Check DB for SUID': checkdb() elif event == 'Submit': updatedb() elif event =='Cancel' or event is None: window.Close() break RE: 2 buttons to run 2 different functions? - JP_ROMANO - Jan-10-2019 Just to follow up - I would not recommend using the code I posted above. Not sure if the problem is the script, python itself, the PySimpleGui library, my environment/setup, or the IDE (Spyder). I can run 3 times and get 3 different results, without changing ANYTHING in the code or input. Python 3.5 crashes after every 2nd script run (not sure what that's all about). Thought I was making progress on this project, but it appears I have to start over and find a more stable way to build a UI with python. I just don't want somebody else potentially seeing and using the solution I posted above. |