Tkinter:无法从绑定输入函数返回值

时间:2016-12-06 18:42:17

标签: python class tkinter

from Tkinter import *

class Calendar(Frame):
  def __init__(self,parent):
    Frame.__init__(self, parent, background="white")

    self.parent = parent
    parent.title("Calendar Application (Individual Project)")#Name of Application
    self.pack(fill=BOTH, expand=1)

    w = 800
    h = 500

    sw = self.parent.winfo_screenwidth()
    sh = self.parent.winfo_screenheight()

    x = (sw - w)/2
    y = (sh - h)/2
    self.parent.geometry('%dx%d+%d+%d' % (w, h, x, y))

    Todo = ['1','2','3','4','5']
    List = Listbox(self)
    for i in Todo:
        List.insert(END,i)

    List.place(x=10,y=130)

    List.bind("<<ListboxSelect>>", self.onSelect)
    self.var = StringVar()

    editbutton = Button(self,text="Edit")
    editbutton.place(x=20,y=300)
    delbutton = Button(self,text="Delete")
    delbutton.place(x=70,y=300)
    Lab1 = Label(self,text = "To-do List",bg='white')
    Lab1.place(x=15,y=105)

  def onSelect(self, val):
    sender = val.widget
    idx = sender.curselection()
    value = sender.get(idx)
    self.var.set(value)
    print(value)

root = Tk()

App = Calendar(root)

mainloop()

我的问题是我似乎无法访问函数(onSelect)之外的值,我计划根据选择的列表对象使按钮具有某种响应。任何帮助都将不胜感激,我已经花了好几个小时,我疯了。

我相应改变了底部,(我认为),得到这个:

def onSelect(self, val):
    sender = val.widget
    idx = sender.curselection()
    self.value = sender.get(idx)   
    self.var.set(self.value)
def EditDel(self,p):
    if p == 1:
        print(p , self.value)
    elif p == 2:
        print(p, self.value)
    else:
        print(p, self.value)

仍然无法正常工作,它说:日历实例没有任何属性&#39;。谢谢,到目前为止所有的帮助,真的很感激。我只是有点失落/ :(

0 个答案:

没有答案