如何在按下<button-1>时使tkinter <enter>事件工作?

时间:2017-12-22 16:07:17

标签: python-3.x events canvas tkinter mouseevent

我想像涂料一样填充矩形。当按下鼠标按钮时,我希望我输入的每个矩形都被填充,否则我不希望发生任何事件。

这是我的代码:

        MemoryStream ms = new MemoryStream();
        const int min = 128; // Grey midpoint
        using (var img = Image.Load(ImagePath))
        using (var texture = Image.Load(Texture))
        {
            if (img.Width > texture.Width || img.Height > texture.Height)
            {
                throw new InvalidOperationException("Image dimensions must be less than or equal to texture dimensions!");
            }

            Image<Rgba32> animated = new Image<Rgba32>(img.Width, img.Height);
            foreach (var Frame in texture.Frames)
            {
                using (var imageFrame = img.Frames.CloneFrame(0))
                {
                    Image<Rgba32> currentFrame = imageFrame;
                    for (int y = 0; y < currentFrame.Height; y++)
                    {
                        for (int x = 0; x < currentFrame.Width; x++)
                        {
                            var pixel = currentFrame[x, y];
                            if (pixel.R >= min && pixel.G >= min && pixel.B >= min && pixel.A >= min)
                            {
                                currentFrame[x, y] = Frame[x, y];
                            }
                        }
                    }
                    animated.Frames.AddFrame(currentFrame.Frames.First());
                }
            }
            GifEncoder GifEncode = new GifEncoder();
            animated.SaveAsGif(ms);
            return ms;

问题在于,当我按下鼠标按钮时,只有按下鼠标的单元格会检测到鼠标指针的入口(也就是最后一个鼠标将被释放的那个)

关于我的代码的任何有益的一般建议当然会非常感激。

1 个答案:

答案 0 :(得分:1)

根本不需要使用//<-- declare empty array to hold data $get = array(); if(!empty($_GET)){ //<-- if the $_GET global array is not empty... run the loop foreach($_GET as $index => $output){ $get[$index] = base64_decode($output); } } var_dump($get); <--- $get now holds the decoded values 。试试:

<Enter>

不幸的是,使用from tkinter import Canvas import tkinter _width = 50 _height = 50 _size = 8 root = tkinter.Tk() root.title("draw me a lovely matrix") canv = Canvas(root, width=_width * _size, height=_height * _size) rects = [] # B1-Motion activates every time the mouse is moved with button 1 held down # See https://effbot.org/tkinterbook/tkinter-events-and-bindings.htm # We fill every item that the mouse comes in contact with while button 1 is held # The tag tied to the item the event was called on, "current", points to the # item under the pointer when the button was pressed, i.e. the first box. # Only the x and y properties change, so we configure the item closest to those. def motion(event): canv.itemconfig(canv.find_closest(event.x, event.y), fill="#aaa") canv.bind("<B1-Motion>", motion) for i in range(_size): for j in range(_size): rects.append(canv.create_rectangle(_width * j, _height * i, _width * (j + 1), _height * (i + 1), fill="#fff", width=0)) # don't need to bind anything to <Enter> canv.pack() root.mainloop() 没有解决方案。如果<Enter>按照您希望的方式工作,那么以下不起作用的代码在理论上将是正确的。

<Enter>

在python 3.8.2,tkinter 8.6中进行了测试