0

I am building a python 2.7.14 GUI and I am using listbox as a menu. The problem I am having is being able to call a function from all of the different menu selections I have listed. I am able to print the selection that I double click on but can not figure out how to call functions from all the different menu selections. Should I not be using listbox for this?

from Tkinter import *


def onselect(evt):
    # Note here that Tkinter passes an event object to onselect()
    w = evt.widget
    index = int(w.curselection()[0])
    value = w.get(index)
    print 'You selected item %d: "%s"' % (index, value)


def diagnostics_1998_2007():
    list_box.delete('0', END)
    list_box.insert(END, '                 1998-2007 DIAGNOSTICS                                       ')
    list_box.insert(END, ' 1.      ALL  (EXCEPT BELOW)                                             ')
    list_box.insert(END, ' 2.      2001 - 2005 Stratus/Sebring Coupe                               ')
    list_box.insert(END, ' 3.      1998 - 2000 Avenger/Sebring Coupe                               ')
    list_box.insert(END, ' 4.      1998 Talon                                                      ')
    list_box.insert(END, ' 5.      2004 - 2007 (RoW) Crossfire                                     ')
    list_box.insert(END, ' 6.      2002 - 2006 Sprinter                                            ')
    list_box.insert(END, '')
    list_box.insert(END, ' Page  1  Of  1                                                              ')
    list_box.insert(END, '')
    list_box.insert(END, ' Press F1 For Help                                                           ')
    list_box.insert(END, ' Press F2 For Sys                                                            ')
    list_box.insert(END, ' Press F3 For Main                                                           ')

#-------------------------------------------------F4 BUTTON-------------------------------------------------------------


def stand_alone_main_menu():
    list_box.delete('0', END)
    list_box.insert(END, '                 STAND-ALONE MAIN MENU                                 ')
    list_box.insert(END, ' 1.      1998 - 2007 Diagnostics                                       ')
    list_box.insert(END, ' 2.      1983 - 1997 Diagnostics                                       ')
    list_box.insert(END, ' 3.      Vehicle Module Scan                                           ')
    list_box.insert(END, ' 4.      Customer Preference                                           ')
    list_box.insert(END, ' 5.      Junction Port Tool                                            ')
    list_box.insert(END, ' 6.      TechTOOLS DataRecorder                                        ')
    list_box.insert(END, '')
    list_box.insert(END, ' Page  1  Of  1                                                        ')
    list_box.insert(END, '')
    list_box.insert(END, ' Press F1 For Help                                                     ')
    list_box.insert(END, '')
    list_box.insert(END, ' Press F3 For Main                                                     ')

#-------------------------------------------------F4 BUTTON-------------------------------------------------------------


def f4():
    list_box.delete('0', END)
    list_box.insert(END, '                             MAIN MENU                                 ')
    list_box.insert(END, ' 1.      DRB III Standalone                                            ')
    list_box.insert(END, ' 2.      Connect to TechTOOlS                                          ')
    list_box.insert(END, ' 3.      Generic Scan Tool                                             ')
    list_box.insert(END, ' 4.      PeP Module Tools                                              ')
    list_box.insert(END, ' 5.      Run Memory Card Program                                       ')
    list_box.insert(END, ' 6.      DRB Utilities                                                 ')
    list_box.insert(END, ' 7.      Legacy MDS1 Support                                           ')
    list_box.insert(END, '')
    list_box.insert(END, ' Page  1  Of  1                                                        ')
    list_box.insert(END, '')
    list_box.insert(END, ' Press F1 For Help                                                     ')

#-------------------------------------------------POWER ON WINDOW-------------------------------------------------------


def power_on():
    list_box.delete('0', END)
    list_box.insert(END, '')
    list_box.insert(END, '                         DRB III Emulator                               ')
    list_box.insert(END, '')
    list_box.insert(END, '                  Version 1.0 - 10/22/2017                              ')
    list_box.insert(END, '')
    list_box.insert(END, '       Enhanced Chrysler Diagnostic Tool                                ')
    list_box.insert(END, '')
    list_box.insert(END, '                                                         ')
    list_box.insert(END, '')
    list_box.insert(END, '                 Press F4 For Main Menu                                 ')


#---------------------------------------------------GUI DISPLAY---------------------------------------------------------

def down():
    list_box.bind("<Down>")
    list_box.yview_scroll(1, 'units')


def up():
    list_box.bind("<Up>")
    list_box.yview_scroll(-1, "units")


root = Tk()
root.title('DRB III EMULATOR')
root.configure(bg='grey10')
root.geometry('392x690')

frame_top = LabelFrame(bg='grey10', fg='yellow', text='Display')
frame_top.grid(row=0, column=0, padx=15, pady=5)

list_box = Listbox(frame_top, bg='cyan', width=36, height=13, font=("Helvetica", 12, "bold"))
list_box.bind('<Double-1>', onselect)
list_box.grid(row=0, column=0, padx=15, pady=15)


#-------------------------------------------------FUNCTION BUTTONS------------------------------------------------------

frame_bottom = LabelFrame(bg='grey10', fg='yellow', text='Keypad')
frame_bottom.grid(row=1, column=0, pady=0)

button_f1 = Button(frame_bottom, bg='grey35', fg='yellow', text='F1', height=2, width=6)
button_f1.grid(row=0, column=0, padx=10, pady=5)

button_f2 = Button(frame_bottom, bg='grey35', fg='yellow',  text='F2', height=2, width=6)
button_f2.grid(row=0, column=1, padx=10, pady=5)

button_f3 = Button(frame_bottom, bg='grey35', fg='yellow', text='F3', height=2, width=6)
button_f3.grid(row=0, column=2, padx=10, pady=5)

button_f4 = Button(frame_bottom, bg='grey35', fg='yellow', text='F4', height=2, width=6)
button_f4.grid(row=0, column=3, padx=10, pady=5)


#----------------------------------------------Arrow Control------------------------------------------------------------

arrow_frame = LabelFrame(bg='grey10', fg='yellow',text='Arrow Control')
arrow_frame.grid(row=4, column=0, padx=5, pady=0)

button_a1 = Button(arrow_frame, bg='grey35', fg='yellow', text='Up', height=2, width=8, command=up)
button_a1.grid(row=0, column=1, padx=5, pady=5)

button_b2 = Button(arrow_frame, bg='grey35', fg='yellow', text='Left', height=2, width=8)
button_b2.grid(row=1, column=0, padx=5, pady=5)

button_b3 = Button(arrow_frame, bg='grey35', fg='yellow', text='Enter', height=2, width=8)
button_b3.grid(row=1, column=1, padx=5, pady=5)

button_b4 = Button(arrow_frame, bg='grey35', fg='yellow', text='Right', height=2, width=8)
button_b4.grid(row=1, column=2, padx=5, pady=5)

button_b5 = Button(arrow_frame, bg='grey35', fg='yellow', text='Down', height=2, width=8, command=down)
button_b5.grid(row=3, column=1, padx=5, pady=5)

#------------------------------------------------CONTROLS FUNCTIONS---------------------------------------------------

function2_frame = LabelFrame(bg='grey10', fg='yellow', text='Controls')
function2_frame.grid(row=5, column=0, padx=5, pady=0)

button_c1 = Button(function2_frame, bg='grey35', fg='yellow', text='Yes', height=2, width=8)
button_c1.grid(row=0, column=0, padx=5, pady=5)

button_c2 = Button(function2_frame, bg='grey35', fg='yellow', text='No', height=2, width=8)
button_c2.grid(row=1, column=0, padx=5, pady=5)

button_c3 = Button(function2_frame, bg='grey35', fg='yellow', text='PageFWD', height=2, width=8)
button_c3.grid(row=0, column=2, padx=5, pady=5)

button_c4 = Button(function2_frame, bg='grey35', fg='yellow', text='PageBCK', height=2, width=8)
button_c4.grid(row=1, column=2, padx=5, pady=5)

button_c5 = Button(function2_frame, bg='grey35', fg='yellow', text='On', height=2, width=8, command=power_on)
button_c5.grid(row=0, column=3, padx=5, pady=5)

button_c6 = Button(function2_frame, bg='grey35', fg='yellow', text='Off', height=2, width=8, command=root.destroy)
button_c6.grid(row=1, column=3, padx=5, pady=5)


root.mainloop()
Qwerty
  • 1,252
  • 1
  • 11
  • 23
Py2018
  • 9
  • 1
  • 1
    See https://stackoverflow.com/q/6554805/7032856. – Nae Feb 14 '18 at 03:12
  • I am already using that exact code it prints selected item but I need to be able to call a function. – Py2018 Feb 14 '18 at 03:17
  • There's an answer there that calls a function as well. – Nae Feb 14 '18 at 03:25
  • Please try to provide a [mcve] as opposed to some code you were working on. – Nae Feb 14 '18 at 09:27
  • Minimal-I had to post all of code shown so whoever was trying to help could understand I could not call a function by index [0] because I am using multiple instances of listbox and probably need to call function by "string somehow.Complete-all of code is here and it will run and reproduce problem.Verifiable-I have described problem in detail,I have eliminated issues not relevant to problem and you can run this code and it will reproduce the issue I have listed. – Py2018 Feb 14 '18 at 11:07
  • Possible duplicate of [Getting a callback when a Tkinter Listbox selection is changed?](https://stackoverflow.com/questions/6554805/getting-a-callback-when-a-tkinter-listbox-selection-is-changed) – Dmitry Feb 14 '18 at 15:46
  • Finally figured it out in the "onselect" function I used - if value == "listbox string": – Py2018 Feb 17 '18 at 04:52

0 Answers0