【问题标题】:Why I can't remove the button from with right click?为什么我不能右键单击删除按钮?
【发布时间】: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


    【解决方案1】:

    pack_forget() 不起作用,因为当您单击并拖动按钮时,您会运行函数MoveWindow(),它使用place() 而不是pack()

    【讨论】:

      【解决方案2】:

      尝试将 self.l.pack_forget 设置为函数,因为目前它不在您的代码中...

      我对 Python 不太熟悉,但我认为它会是这样的 -

      Function self.l.pack_forget() {
        //your code here
      }
      

      【讨论】:

        猜你喜欢
        • 2011-09-13
        • 2012-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多