【发布时间】:2018-06-15 03:34:36
【问题描述】:
我尝试使用绑定方法来使用右键单击删除按钮“l”但是当我按下它时它不起作用,而且如果我打印一个像这样的单词
self.l.bind('<Button-3>',lambda x: print("hello"))
如果我只尝试打印一条消息,它是有效的,但在其他情况下则不行。
self.l.bind('<Button-3>',lambda x: self.l.pack_forget())
有什么帮助吗?
from tkinter import *
def clamp(lo, hi, x):
return min(max(x, lo), hi)
class blah:
all = []
def MoveWindowStart(self, event):
self.move_lastx = event.x_root
self.move_lasty = event.y_root
self.focus()
def MoveWindow(self, event):
dx = event.x_root - self.move_lastx
dy = event.y_root - self.move_lasty
self.move_lastx = event.x_root
self.move_lasty = event.y_root
self.x = clamp(0, 20, self.x + dx) # should depend on
self.y = clamp(0, 20, self.y + dy) # actual size here
self.l.place_configure(x=self.x, y=self.y)
def __init__(self, root,title, x,y):
self.root = root
self.x = x; self.y = y
self.l = Button(self.root, bd=1, bg="#08246b", fg="white",text=title,height = 5, width = 5)
self.l.pack()
self.l.bind('<1>', self.MoveWindowStart)
self.l.bind('<B1-Motion>', self.MoveWindow)
self.l.bind('<Button-3>',lambda x: self.l.pack_forget())
self.all.append(self)
self.focus()
def focus(self, event=None):
self.l.tkraise()
for w in self.all:
if w is self:
w.l.configure(bg="#08246b", fg="white")
else:
w.l.configure(bg="#d9d9d9", fg="black")
def newbutton():
x = blah(root, "Window 1",320, 0)
root = Tk()
root.title("...")
root.geometry("%dx%d%+d%+d"%(640, 480, 0, 0))
j = Button(root, bd=1, bg="#08246b", fg="white",text='tifdgfdgdftle',command=newbutton)
j.pack()
root.mainloop()
【问题讨论】:
标签: python button tkinter right-click