停止Tkinter菜单自动运行命令

时间:2012-12-09 22:53:20

标签: python python-3.x tkinter

我正在学习python,本周我获得了GUI编码的基础知识。

我遇到的问题是底部的 f1handler1(),请您帮我理解如何使菜单命令等到第一次点击相关的菜单项?目前,只要我执行二次鼠标单击以调出菜单,它就会在我选择菜单之前自动删除所选项目:( (但出于某种原因退出选项不?)

非常感谢。
(抱歉,如果代码难以阅读,这是我的第一语言和GUI,我正在从我正在进行的课程和大量的网络搜索中慢慢学习。)

from tkinter import *

ALL = N+S+W+E

class Application(Frame):

    def __init__(self, master=None):
        #initiate the primary window.
        Frame.__init__(self, master)
        self.master.rowconfigure(0, weight=1)
        self.master.columnconfigure(0, weight=1)

        self.rowconfigure(0, weight=0)
        self.rowconfigure(1, weight=0)
        self.rowconfigure(2, weight=3)
        self.columnconfigure(0, weight=0)
        self.columnconfigure(1, weight=1)
        self.columnconfigure(2, weight=1)

        self.grid(sticky=ALL)
        self.frameset()

    def frameset(self):
        #define and setup frames with columns and rows for widgets
        #Colours added to framesets to help designing layout. delete them
        self.Frame1 = Frame(self, bg='blue')   # D
        self.Frame2 = Frame(self, bg='green')  # E
        self.Frame3 = Frame(self)              # L
        self.Frame4 = Frame(self, bg='green')  # E
        self.Frame5 = Frame(self, bg='orange') # T
        self.Frame6 = Frame(self, bg='yellow') # E colours

        self.Frame1.rowconfigure(0,weight=0)
        self.Frame2.rowconfigure(0,weight=0)
        self.Frame3.rowconfigure(0,weight=1)
        self.Frame4.rowconfigure(0,weight=1)
        self.Frame5.rowconfigure(0,weight=1)
        self.Frame6.rowconfigure(0,weight=1)

        self.Frame1.columnconfigure(0,weight=0)
        self.Frame2.columnconfigure(0,weight=0)
        self.Frame3.columnconfigure(0,weight=1)
        self.Frame4.columnconfigure(0,weight=1)
        self.Frame5.columnconfigure(0,weight=1)
        self.Frame6.columnconfigure(0,weight=1)

        self.Frame1.grid(row=0, column=0, rowspan=1, columnspan=1, sticky=ALL)
        self.Frame2.grid(row=0, column=1, columnspan=2, sticky=ALL)
        self.Frame3.grid(row=1, column=0, rowspan=2, sticky=ALL)
        self.Frame4.grid(row=1, column=1, columnspan=2, sticky=ALL)
        self.Frame5.grid(row=2, column=1, rowspan=1, columnspan=1, sticky=ALL)
        self.Frame6.grid(row=2, column=2, sticky=ALL)


        label4a = Label(self.Frame4, text='Accounts', bg='orange')
        label4b = Label(self.Frame4, text='Recent Payroll', bg='yellow')
        label4a.pack(side=LEFT)
        label4b.pack(side=RIGHT)

        self.objects()

    def objects(self):
        self.f3ListBox = Listbox(self.Frame3, selectmode='single')
        self.f3ListBox.insert(1,'Colchester 441')
        self.f3ListBox.insert(2,'Chelmsford 914')
        self.f3ListBox.insert(3,'London 123')
        self.f3ListBox.grid(sticky=ALL)
        self.f3ListBox.bind("<Button-3>", self.f1handler1)

        f5ListBox = Listbox(self.Frame5, selectmode='single')
        f5ListBox.insert(0,'Fred Asus')
        f5ListBox.insert(1,'Tom Yahoo')

        f5ListBox.grid(sticky=ALL)

        f6ListBox = Listbox(self.Frame6, selectmode='single')
        f6ListBox.insert(1,'S123456') # DELETE
        f6ListBox.grid(sticky=ALL)

        #Dropdown menu to use on the top left corner        
        var = StringVar()
        var.set('Costcode')
        dmenu1 = OptionMenu(self.Frame1,var, 'Costcode','Name')
        dmenu1.pack(side=TOP, fill=BOTH)

    def f1handler1(self,event):
        """Creates a popup menu for the alternative mouse button.
        Edit this to add more options to that popup"""
        select = self.f3ListBox.delete(ACTIVE)
        popup = Menu(self, tearoff=0)
        popup.add_command(label='Quit',command=self.quit)
        popup.add_command(label='delete',command=select) #add more of these for more options


        try:
            popup.post(event.x_root, event.y_root)
        except:
            pass


root = Tk()
app = Application(master=root)
app.mainloop()

2 个答案:

答案 0 :(得分:7)

问题实际上是在添加命令之前,

select = self.f3ListBox.delete(ACTIVE)

执行此操作后,它会执行删除操作并将结果分配给select。你应该做点什么

select = lambda: self.f3ListBox.delete(ACTIVE)

创建一个在适当的时间调用delete的函数。然后,您可以将select作为菜单项的命令传递。

答案 1 :(得分:-2)

有时在tkinter中设置按钮命令时,会自动调用该命令,而不是等待单击。 可能的解决方案可能是添加lambda

popup.add_command(label='Quit',command= lambda: self.quit)