2

我正在学习 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()
4

2 回答 2

7

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

select = self.f3ListBox.delete(ACTIVE)

一旦执行此操作,它就会执行删除操作并将其结果分配给select. 你应该做类似的事情

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

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

于 2012-12-10T01:36:25.810 回答
-1

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

popup.add_command(label='Quit',command= lambda: self.quit)
于 2012-12-09T23:17:11.980 回答