简体   繁体   English

如何在Tkinter的标签上画圆?

[英]How to draw a circle on a label on Tkinter?

i tried to draw a circle (its supposed to be a mouse cursor) on a label that has an image in it. 我试图在其中有图像的标签上绘制一个圆(应该是鼠标光标)。 the circle position needs to be changed every time i get it on the sockets i noted the mouse poisition with ** on my code i dont know how to do it, i will be glad if you help me, thanks a lot 每次我将其插入插座时,都需要更改圆的位置,我在代码中用**标记了鼠标的位置,我不知道该怎么做,如果您能帮助我,我将非常高兴,非常感谢

        import socket
    from PIL import Image
    import StringIO
    import Tkinter
    from PIL import Image
    from PIL import ImageTk
    import threading


    RECV_BLOCK=1024

    s=socket.socket()
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.connect(("127.0.0.1",12345))


    im = None
    def ShowImage():
            root = Tkinter.Tk()
            label = Tkinter.Label(root)
            label.pack()
            img = None
            tkimg = [None]  # This, or something like it, is necessary because if you do not keep a reference to PhotoImage instances, they get garbage collected.



            delay = 2   # in milliseconds
            def loopCapture():
                print "capturing"
    #    img = fetch_image(URL,USERNAME,PASSWORD)
                global im
                while im==None:
                        continue
                img = im
                tkimg[0] = ImageTk.PhotoImage(img)
                label.config(image=tkimg[0])
                root.update_idletasks()
                root.after(delay, loopCapture)

            loopCapture()
            root.mainloop()

    def rcvimage():
            global im
            for i in range(1000):
                    data=''

                    size=s.recv(RECV_BLOCK)
                    s.send(size)
                    size=int(size)

                    while  True:
                            buf=s.recv(RECV_BLOCK)
                            data+=buf

                            if len(data)>=size:
                                    break

                    pic =data[:data.find("$$$$$$")]
                    mouse=data[data.find("$$$$$$")+6:] # **the position of the cursor is here - for example ("125$200") - the first number is x, and the second is y**
                    print mouse
                    try:

                            print(len(pic))
                            f=StringIO.StringIO(pic)
                            global im
                            im=Image.open(f)
                            #ShowImage(im)
                            #im.show()
                            s.send ("next")
                    except Exception as e:
                            s.send("fail:"+e.message)
                            break

            print "End"
    thread2 = threading.Thread(target = rcvimage)
    thread1 = threading.Thread(target = ShowImage)

    thread1.start()
    thread2.start()

You cannot draw an image on top of a label that already has an image. 您不能在已经有图像的标签上绘制图像。 You can change the cursor itself with the configure method. 您可以使用configure方法更改光标本身。

However, if you use a very small canvas rather than a label, you can draw on top of text that is added to the canvas. 但是,如果使用很小的画布而不是标签,则可以在添加到画布上的文本上绘制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM