1

I want the text of the button to move just like marquee text when the cursor is over the button. I'm using Marquee widget from tkmacosx library which has the functionality to move the text when the cursor is over the text but here i want the same with the button.

How can i move the text of the button or is there any alternative for this? Please help

Sample code

from tkinter import *
from tkmacosx import *

text = """Please hover over the text to move it. \
This text will move only if the cursor hovers over \
the text widget."""

root = Tk()
root['bg'] = '#333'
m = Marquee(root, fg='#FEE715', bg='#101820', text=text)
m.pack(pady=10, padx=10)
button = Button(root, borderless=1, bg='#101820', fg='#FEE715')
button.pack()
button.bind('<Enter>', lambda _: m.play())
button.bind('<Leave>', lambda _: m.stop(True))
root.mainloop()

I took the example from the documentation of Marquee widget. Thanks in advance.

  • What you are trying is not possible but instead you can create a function loop with [after](https://www.geeksforgeeks.org/python-after-method-in-tkinter/) that will change the text in such a way that it will look like marquee text, you can also take references from this [answer](https://stackoverflow.com/a/47224514/10364425). – Saad Sep 24 '20 at 14:27

1 Answers1

0

You can easily do this by putting the text which should be marquee inside the button. Here is the code to do this.

from tkinter import *
from tkmacosx import Marquee

text1 = """This text will move from right to left \
if does not fit the window."""
text2 = """Please hover over the text to move it. \
This text will move only if the cursor hovers over \
the text widget."""

root = Tk()
root['bg'] = '#333'
Marquee(root, bg='#FEE715', fg='#101820', text=text1).pack(pady=10)
button=Button(root, bg='#FEE715', fg='#101820', text=text1)
button.pack()
m = Marquee(button, fg='#FEE715', bg='#101820', text=text2)
m.pack(pady=(0,10), padx=10)
m.bind('<Button-1>',lambda e:print("Please try to upvote if the answer is right and tick as right answer . Thankyou"))
m.stop(True)
m.bind('<Enter>', lambda _: m.play())
m.bind('<Leave>', lambda _: m.stop())
root.mainloop()
imxitiz
  • 3,920
  • 3
  • 9
  • 33
  • thanks kshitiz but in your code, the text is not aligned and when the button is pressed you can't see the pressed button completely as the marquee widget is in the way. – user14323200 Sep 23 '20 at 07:55
  • Also there are problems in clicking the button same reason as the marquee widget is in the way. – user14323200 Sep 23 '20 at 07:56
  • Ok, Thanks for the info. I will try to solve it and inform you. :) – imxitiz Sep 24 '20 at 19:56
  • Yup It's is very simple. Just bind that marquee with click button( or or other any event) you can do same as in button also. So that both will do the same work. And already mentioned Please Try to up vote if the answer is correct and tick as right answer. Hope It will work well. :) – imxitiz Sep 24 '20 at 20:04