0

I've just started playing around with the python tkinter library and I ran into some problems just following online tutorials to the letter. I can't seem to change the colour of anything (button, text, cursor...). I've tried this on two different computers and both times I get the same result. I'm using the anaconda distribution and I added tkinter with the "conda install tkinter" command.

Working example:

from tkinter import * 
from tkinter.ttk import *
  

master = Tk()
master.geometry("500x500")
master.config(cursor="dot red")
  

mainloop()

I've even tryerd:

from tkinter import * 
from tkinter.ttk import *
  

master = Tk()
master.geometry("500x500")
master.config(cursor="dot #ff0000")
  

mainloop()

Bot produce the same exact result (I'm really sorry for taking the picture of the screen with my phone, but the cursor wouldn't show up on a print screen) : Cursor picture

Thanks for all of your help.

Kevx555
  • 1
  • 1
  • Try adding `root.config(cursor="none")` then you will need to design and implement code that shows a sprite that follows the mouse's position – TheLizzard Jun 22 '21 at 14:17

3 Answers3

2

I did a pretty quick search and found that the cursor color isn't supported in Windows according to this site:

https://www.geeksforgeeks.org/how-to-change-the-color-and-symbol-of-the-cursor-in-tkinter/

However, if you aren't using windows on both of those computers I am not sure what could be causing the problem.

Azyru
  • 43
  • 1
  • 6
  • I can't believe I missed that it's not supported by windows. Would you know if there's any better GUI for Python to use with windows? – Kevx555 Jun 22 '21 at 22:46
  • Kevx555, I haven't really messed with many UI libraries but I've heard PyQt5 mentioned quite a bit with some pretty good reviews. I also found this question that may allow you to accomplish your original goal, though I don't have the personal experience with PyQt to confirm. https://stackoverflow.com/questions/36725245/pyqt-changing-cursor-when-hovering-a-button – Azyru Jun 23 '21 at 16:35
0

As the other answer pointed out, there doesn't seem to be a way to change the cursor's colour on Windows but you can remove the cursor using .config(cursor="none") and add your own sprite that follows the mouse like this:

import tkinter as tk


class CanvasWithColouredCursor(tk.Canvas):
    def __init__(self, master, **kwargs):
        super().__init__(master, **kwargs)

        self.cusor_sprite_id = None

        super().config(cursor="none")

        super().bind("<Enter>", self.show_sprite)
        super().bind("<Leave>", self.delete_sprite)
        super().bind("<Motion>", self.move_sprite)

    def show_sprite(self, event:tk.Event=None):
        self.delete_sprite()
        pos = event.x, event.y
        self.cusor_sprite_id = self.create_circle(*pos, r=5, fill="red",
                                                  outline="red")

    def move_sprite(self, event:tk.Event):
        if self.cusor_sprite_id is None:
            return None
        pos = event.x, event.y
        super().moveto(self.cusor_sprite_id, *pos)

    def delete_sprite(self, event:tk.Event=None):
        if self.cusor_sprite_id is not None:
            super().delete(self.cusor_sprite_id)
            self.cusor_sprite_id = None

    def create_circle(self, x, y, r, **kwargs):
        # Taken from: https://stackoverflow.com/a/17985217/11106801
        return super().create_oval(x-r, y-r, x+r, y+r, **kwargs)


root = tk.Tk()

canvas = CanvasWithColouredCursor(root)
canvas.pack()

root.mainloop()

This is just to show the concept, if you move the mouse very fast the sprite get's distorted.

TheLizzard
  • 7,248
  • 2
  • 11
  • 31
0

I'm so sorry didn't see that it's not supported in windows. Are there any better well-documented GUI for python with full windows supprt?

Kevx555
  • 1
  • 1