gpt4 book ai didi

python - 停止 Tkinter 菜单自动运行命令

转载 作者:行者123 更新时间:2023-11-28 22:01:04 32 4
gpt4 key购买 nike

我正在学习 Python,这周我学习了 GUI 编码的基础知识。

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

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

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()

最佳答案

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

select = self.f3ListBox.delete(ACTIVE)

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

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

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

关于python - 停止 Tkinter 菜单自动运行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13792743/

32 4 0