Bottom Page

Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
2 buttons to run 2 different functions?
#1
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()
Quote
#2
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   
Quote
#3
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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Buttons not appearing, first time button making admiral_hawk 5 278 Dec-31-2018, 03:26 AM
Last Post: admiral_hawk
  [WxPython] setting two buttons next to each other metulburr 9 596 Aug-14-2018, 04:32 PM
Last Post: Windspar
  Problem with buttons Epilepsy 5 689 May-12-2018, 11:28 PM
Last Post: Larz60+
  Help in a Go game buttons notreallyapro 5 593 May-12-2018, 09:29 PM
Last Post: notreallyapro
  [Tkinter] How do you create buttons and what is or how does super work? Fox_cutter12 2 483 May-10-2018, 08:14 PM
Last Post: notreallyapro
  [Tkinter] Aligning Python Tkinter Buttons In Rows TollyThaWally 3 1,392 Feb-22-2018, 02:56 AM
Last Post: woooee
  [Tkinter] Newbie, need help with Buttons owyvan 1 575 Dec-18-2017, 02:53 PM
Last Post: Windspar
  Multi windows in tkinter buttons not responding correctly curtjohn86 13 3,042 Jul-01-2017, 03:42 PM
Last Post: nilamo
  [Tkinter] How do I get buttons on top of an image? Saif133 4 2,493 Jan-04-2017, 04:20 PM
Last Post: Saif133
  [Tkinter] selecting buttons with keyboard noissue 3 2,058 Oct-05-2016, 12:51 PM
Last Post: noissue

Forum Jump:


Users browsing this thread: 1 Guest(s)