在鼠标释放后调用python中的函数&从selcting选项后关闭tkinter窗口

时间:2018-05-15 09:50:45

标签: python tkinter

我想在鼠标释放按钮后调用python中的一个函数。目前我在调用键后调用相同的函数' a'按下。我也想在选择一个选项后自动关闭tkinter窗口。这是我的代码:

def onkeypress1(event):
 if event.key == 'a':
  def option_changed(*args):
     c=variable.get()
     object_list.append(c)
     print c
     return c


  master = Tk()
  variable = StringVar(master)
  variable.set("select") # default value
  variable.trace("w", option_changed)

  w = OptionMenu(master, variable, "INDIA", "USA", "UK")
  w.pack()

  mainloop()

我想在鼠标释放按钮后调用函数onkeypress1()。

2 个答案:

答案 0 :(得分:1)

您可以为ButtonRelease事件创建绑定。像你这样的问题已在这里得到解答:How can I identify when a Button is released in Tkinter?

答案 1 :(得分:0)

- 鼠标释放后调用一个函数,选择一个选项后关闭tkinter

from tkinter import Tk, Frame, OptionMenu, StringVar

class App(Frame):
    '''Class App'''
    def __init__(self, master=None):
        """ init App """
        Frame.__init__(self, master, bg="dim grey")
        self.bind("<ButtonRelease-1>", self.onkeypress1) # call function after mouse release
        self.pack(ipadx=50,ipady=50)
        self.variable = StringVar()
        self.variable.set("Select") # default value

        #trace(mode, callback) mode 'w' = call if variable is written
        self.variable.trace("w", self.option_changed) 

        w = OptionMenu(self, self.variable, "INDIA", "USA", "UK")
        w.pack()

    def onkeypress1(self,event):
        print("jo")

    def option_changed(self, *args):
        c=self.variable.get()
        print(c)
        if self.variable != "Select":
            self.master.destroy()

if __name__ == '__main__':
    root = Tk()
    app = App(root)
    root.mainloop()
    print("it runs further")