从python中的菜单传递值

时间:2017-04-20 19:33:08

标签: python-3.x

寻找捷径,如果有的话。下面是我正在使用的代码的一部分,今晚我还要添加更多其他菜单选项。

除了通过转到单独的函数并设置值并调用执行操作的main函数之外,是否有更短的设置值的方法。可以直接从menu.add行完成吗?

    menubar = Menu(root)
    a = Menu(menubar, tearoff=0)
    b = Menu(menubar, tearoff=0)
    c = Menu(menubar, tearoff=0)
    d = Menu(menubar, tearoff=0)
    e = Menu(menubar, tearoff=0)
    a.insert_cascade(0, label = 'Option 1', menu=b)
    b.add('checkbutton', label = 'Choice 1', command = self.f1)
    b.add('checkbutton', label = 'Choice 2', command = self.f2)
    a.insert_cascade(1, label = 'Option 2', menu=c)
    c.add('checkbutton', label = 'Choice 1', command = self.g3)
    c.add('checkbutton', label = 'Choice 2', command = self.g4)
    a.insert_cascade(2, label = 'Option 3', menu=d)
    d.add('checkbutton', label = 'Choice 1', command = self.h1)
    d.add('checkbutton', label = 'Choice 2', command = self.h2)
    d.add('checkbutton', label = 'Choice 3', command = self.h5)
    a.insert_cascade(3, label = 'Option 4', menu=e)
    e.add('checkbutton', label = 'Choice 1', command = self.i1)
    e.add('checkbutton', label = 'Choice 2', command = self.i2)
    e.add('checkbutton', label = 'Choice 3', command = self.i5)
    menubar.add_cascade(label = 'Routines', menu=a)

def f1(self):
    self.sttype = 1
    self.routine()

def f2(self):
    self.sttype = 2
    self.routine()

def g3(self):
    self.sttype = 3
    self.routine()

def g4(self):
    self.sttype = 4
    self.routine()

def h1(self):
    self.sttype = 1
    self.routine()

def h2(self):
    self.sttype = 2
    self.routine()

def h5(self):
    self.sttype = 5
    self.routine()

def i1(self):
    self.sttype = 1
    self.routine()

def i2(self):
    self.sttype = 2
    self.routine()

def i5(self):
    self.sttype = 5
    self.routine()

0 个答案:

没有答案