Python 为什么我能';t右键单击是否无法从中删除按钮?
我尝试使用绑定方法来使用鼠标右键单击删除按钮“l”,但当我按下按钮时,它不起作用,而且如果我打印一个单词,如Python 为什么我能';t右键单击是否无法从中删除按钮?,python,button,tkinter,right-click,Python,Button,Tkinter,Right Click,我尝试使用绑定方法来使用鼠标右键单击删除按钮“l”,但当我按下按钮时,它不起作用,而且如果我打印一个单词,如 self.l.bind('<Button-3>',lambda x: print("hello")) self.l.bind(“”,lambda x:print(“你好”)) 如果我只想打印一条消息,它是有效的,但在另一种情况下没有 self.l.bind('<Button-3>',lambda x: self.l.pack_forget()) se
self.l.bind('<Button-3>',lambda x: print("hello"))
self.l.bind(“”,lambda x:print(“你好”))
如果我只想打印一条消息,它是有效的,但在另一种情况下没有
self.l.bind('<Button-3>',lambda x: self.l.pack_forget())
self.l.bind(“”,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()
从tkinter导入*
def卡箍(低、高、x):
返回最小值(最大值(x,lo),hi)
课堂废话:
全部=[]
def MoveWindowStart(自身、事件):
self.move\u lastx=event.x\u root
self.move\u lasty=event.y\u root
self.focus()
def移动窗口(自身、事件):
dx=event.x\u root-self.move\u lastx
dy=event.y\u root-self.move\u lasty
self.move\u lastx=event.x\u root
self.move\u lasty=event.y\u root
self.x=夹具(0,20,self.x+dx)#应取决于
self.y=夹具(0,20,self.y+dy)#此处的实际尺寸
self.l.place\u configure(x=self.x,y=self.y)
定义初始化(self,root,title,x,y):
self.root=根
self.x=x;self.y=y
self.l=按钮(self.root,bd=1,bg=“#08246b”,fg=“白色”,text=title,height=5,width=5)
self.l.pack()
self.l.bind(“”,self.moveWindowsStart)
self.l.bind(“”,self.MoveWindow)
self.l.bind(“”,lambda x:self.l.pack_-forget())
self.all.append(self)
self.focus()
def焦点(自身,事件=无):
self.l.tkraise()
对于w in self.all:
如果w是self:
w、 l.配置(bg=“#08246b”,fg=“白色”)
其他:
w、 l.configure(bg=“#d9d9d9”,fg=“黑色”)
def newbutton():
x=blah(根,“窗口1”,320,0)
root=Tk()
root.title(“…”)
根几何体(“%dx%d%+d%+d”%(640480,0,0))
j=按钮(根,bd=1,bg=“#08246b”,fg=“白色”,text='TIFDGDGDFTLE',command=newbutton)
j、 包()
root.mainloop()
尝试将self.l.pack\u forget设置为函数,因为到目前为止,它不在您的代码中
我对Python不太熟悉,但我认为它应该是这样的-
Function self.l.pack_forget() {
//your code here
}
pack\u-forget()
不起作用,因为当您单击并拖动按钮时,运行函数MoveWindow()
,该函数使用place()
而不是pack()