Python2.7使用多个列表框作为菜单,需要从选择中调用函数

2024-04-20 12:42:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在构建一个python2.7.14gui,并使用listbox作为菜单。我遇到的问题是能够从我列出的所有不同菜单选项中调用函数。我可以打印选择,我双击,但不能想出如何调用所有不同的菜单选择功能。我不应该使用列表框吗?你知道吗

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()

Tags: textboxcolumnbuttonframelistgridend