【问题标题】:Is there a way to click all the buttons on which i clicked and dragged my mouse in tkinter有没有办法单击我在 tkinter 中单击并拖动鼠标的所有按钮
【发布时间】:2020-04-29 10:14:06
【问题描述】:

我创建了一个按钮网格,我需要通过单击并拖动按钮来更改按钮的背景颜色。 这是因为网格非常庞大,单独单击每个按钮很乏味。

【问题讨论】:

  • 您是否尝试过 bind() <B1-Motion><Enter> 之类的事件?
  • 我无法为我的示例找到使用 事件的正确方法

标签: python python-3.x tkinter mouseevent


【解决方案1】:

您可以绑定<B1-Motion> 以在鼠标被拖动时捕获事件。在绑定函数中,您可以使用winfo_containing 方法来确定光标下的小部件。

例子:

import tkinter as tk

root = tk.Tk()
for row in range(20):
    for column in range(20):
        f = tk.Frame(root, width=20, height=20, bd=1, relief="raised")
        f.grid(row=row, column=column, sticky="nsew")

root.grid_columnconfigure("all", weight=1)
root.grid_rowconfigure("all", weight=1)

def paint(event):
    widget = event.widget.winfo_containing(event.x_root, event.y_root)
    if widget:
        widget.configure(bg="green")

root.bind("<B1-Motion>", paint)
root.mainloop()

【讨论】:

    【解决方案2】:

    编辑:我几个小时前就做到了,但我不得不做一些不同的事情,同时 Bryan Oakley 也提出了同样的想法 :) 现在我知道这是个好主意。

    但是这种方法会选择所有小部件 - 即使您不想要它 - 所以我添加了自己的属性 selectable 并使用 hasattr() 所以只能选择一些小部件。


    我在root 上使用&lt;B1-Motion&gt; 运行功能,该功能使用鼠标位置和winfo_containing 来识别小部件并更改其背景。

    import tkinter as tk
    
    def select(event):
        widget = root.winfo_containing(event.x_root, event.y_root)
        if widget and hasattr(widget, 'selectable'):
            widget['bg'] = 'red'
    
    root = tk.Tk()
    
    for i in range(9):
        for e in range(9):
            b = tk.Button(root, text="X")
            b.selectable = True
            b.grid(row=e, column=i)
    
    root.bind('<B1-Motion>', select)
    
    l = tk.Label(root, text="Label which can't be selected")
    l.grid(row=9, column=0, columnspan=9)
    b = tk.Button(root, text="Button which can't be selected")
    b.grid(row=10, column=0, columnspan=9)
    e = tk.Entry(root)
    e.insert('end', "Entry which can't be selected")
    e.grid(row=11, column=0, columnspan=9)
    
    root.mainloop()
    

    【讨论】:

      【解决方案3】:

      我假设您使用 range() 函数循环以创建所有网格按钮。

      在以下 OOP 代码中,您都可以一键更改所有内容, 还可以将鼠标悬停在每种颜色上并更改颜色:

      注意:

      • 点击白色按钮会将所有内容变为绿色
      • 将鼠标悬停在每种颜色上会将其变为蓝色
      from tkinter import *
      
      
      root = Tk()
      
      
      def change_myself(button,color_in_hex):
          button.configure(bg = color_in_hex)
      
      
      class MyButtons():
          def __init__(self,current_color,new_color,button_amount):
              super(MyButtons,self).__init__()
              self._current_color = current_color
              self._new_color = new_color
              self._button_amount = button_amount
              self._buttons_list = list(range(self._button_amount))
      
      
          @property
          def buttons_list(self):
              return self._buttons_list
      
          def __getitem__(self, item):
              return item
      
          def __setitem__(self, key, value):
              self._buttons_list = value
      
      
          def create_buttons(self):
              for a in range(self._button_amount):
                  self.buttons_list[a] = Button(root,bg = self._current_color,text = 'Button Number {0}'.format(a))
                  print(self.buttons_list[a])
                  self.buttons_list[a].grid(row = a,column = 0)
                  self.buttons_list[a].bind('<Enter>',lambda event,current_button_index = a: self._buttons_list[current_button_index].configure(bg = '#0000FF'))
      
              special_button = Button(root,bg = '#FFFFFF',text = 'Changer!')
              special_button.grid(row = 0,column = 1)
              special_button.bind('<Button-1>',lambda event: self.change_everything())
      
          def change_everything(self):
              for button in self.buttons_list:
                  button.configure(bg = self._new_color)
      
      implementation = MyButtons(current_color = '#FF0000',new_color = '#00FF00',button_amount = 10)
      implementation.create_buttons()
      
      root.mainloop()
      

      结果如下:

      【讨论】:

      • 这并没有解决想要拖动按钮更改其颜色的问题。
      • 我想要的是,考虑到您的示例,当我左键单击 0 号按钮并将鼠标拖动到 6 号按钮然后松开鼠标时,所有 0-6 号按钮都应该改变颜色。跨度>
      • 嘿@Bryan Oakley,我进行了质量检查并编辑了我的答案,请考虑检查我的编辑并让我知道你的想法。
      • @kausrikrao 为之前的错误答案道歉,请现在检查此代码并让我知道它的行为。 :)
      • @JimErginbash 感谢您的回答,但我希望在您拖动按钮而不是悬停在按钮上时改变背景颜色。布莱恩的回答解决了我的问题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多