Tkinter active fill by tag - python

I'm designing a GUI application using Tkinter and for this project, I need buttons for the menu. While looking into the buttons I wasn't blown away by the customization options that come with the buttons, especially when I found out that you can bind click arguments to rectangles.
This allows me to customize the "button" in (almost) limitless ways, but to allow me to put text on the button I need to create a rectangle element and a text element and bind them together using Tkinter's tag_bind property.
One of the design properties of the button that I wanted was active fill when the user moused over the element. Right now I'm just using activefill="" which works, except the text element and the button element will only fill while the mouse is over that element. So, for example, when I mouse over the button the button excluding the text will highlight and vise versa when I mouse over the text.
Below is a simplified (for brevity) version of what I use to generate the buttons;
button = canvas.create_rectangle(button_width, button_height, 10, 10, fill="000", activefill="111", tags="test")
text = canvas.create_text((button_width/2), (button_height/2), activefill="111", tags="test")
canvas.tag_bind("test", "<Button-1>", "foo")
Is there a way to bind the active fill function to a tag rather than a specific element?
Another option is that I completely missed a bunch of information about customizing the buttons in Tkinter, and I would not be apposed to learning about that.

Option 1
I would personally not go for the presented solution. I do not know if you are using the button provided by tk or ttk. But, with the tkinter.tk, you could absolutely change the appearance of the button.
Following, I give you an example that produces a button with the following characteristics:
Blue foreground
Flat appearance
When hovered, the background is green
When pressed, the background is red
The code is as follows:
import tkinter as tk
root = tk.Tk()
# Function hovering
def on_enter(e):
btn['background'] = 'green'
def on_leave(e):
btn['background'] = 'white'
# Create the button
btn = tk.Button(root, background='white', activebackground='red', foreground='blue',relief='flat',text='Test',width=20)
btn.pack()
# Bindings
btn.bind("<Enter>", on_enter)
btn.bind("<Leave>", on_leave)
# Loop
root.mainloop()
Option 2
If even after having tried the tk.Button, you are not glad with the result, I would create a Frame containing a Label (you can do nearly anything with that combination). Then, you could change the background of the frame according to any user action, like:
import tkinter as tk
root = tk.Tk()
# Function hovering
def on_enter(e):
lab['background'] = 'green'
def on_leave(e):
lab['background'] = 'white'
# Click
def on_click(e):
print("hi")
# Create the frame with a label inside
fr = tk.Frame(root)
lab = tk.Label(fr, text="Test", width=20, background="white")
# Packing
fr.pack()
lab.pack()
# Bindings
fr.bind("<Enter>", on_enter)
fr.bind("<Leave>", on_leave)
lab.bind("<Button-1>", on_click)
# Loop
root.mainloop()
You could even create a class with the above combination.

Related

Want to change color of 100 buttons on hover in tkinter

import tkinter as tk
def on_enter(e):
year_btn.config(background="orange",foreground="white")
def on_leave(e):
year_btn.config(background="white", foreground="black")
window = tk.Tk()
yearnumber=1
for i in range(10):
window.rowconfigure(i,weight=1,minsize=40)
window.columnconfigure(i,weight=1,minsize=40)
for j in range(10):
frame = tk.Frame(padx=5,pady=5)
frame.grid(row=i,column=j,sticky="nsew")
year_btn = tk.Button(text=f"{yearnumber}", master=frame, activebackground="red", activeforeground="white")
year_btn.pack(padx=1, pady=1,fill="both",expand="true")
#year_btn.grid(sticky="nsew")
yearnumber+=1
year_btn.bind('<Enter>', on_enter)
year_btn.bind('<Leave>',on_leave)
window.mainloop()
So, I created hundred buttons over here and wanted them to change color when the mouse hovers over them, I did this as per the internet to create events and bind them with the buttons.
My problem is I created hundred buttons using for-loop, so I added the binding code in the loop. The result of this was that if I hover the mouse over any Button only the 100th hover changes color. I also placed the hovering code outside the loop but that does nothing
How do I change color of button over hover for each button in this case.
Thank you
The event object that is passed to the bound function has a reference to the widget that received the event, under the attribute widget. You can use that to change the attribute of the button.
def on_enter(e):
e.widget.config(background="orange",foreground="white")
#^^^^^^^
def on_leave(e):
e.widget.config(background="white", foreground="black")
#^^^^^^^

How do I efficiently change all the buttons in my program?

I have coded multiple buttons in my Tkinter program, and I want to change the text colour of all of them to blue. I know I could type 'fg="blue"'every time I create a new button, but I'm looking for a way to select all the buttons in my program, and change the background colour of all of them at the same time.
So far I've tried
for AllButtons in (Button1, Button, ect.)
But it still takes a long time and I'll have to add to the list every time I make a new button. What's the most efficient way of changing the text colour of all the buttons in my program?
You can use ttk widgets and their style to change the appearance of all of the widgets of a specific class at once.
Consider the following example, clicking on the change style button will change the text color from red to blue.
import tkinter as tk
from tkinter import ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('300x110')
self.resizable(0, 0)
self.style = ttk.Style(self)
self.style.configure('W.TButton', foreground = 'red')
# login button
login_button = ttk.Button(self, text="Login",style="W.TButton")
login_button.grid(column=1, row=1, sticky=tk.E)
change_button = ttk.Button(self, text="Change Style",style="W.TButton", command=self.changeStyle)
change_button.grid(column=1,row=2,sticky=tk.E)
def changeStyle(self):
print("Change")
self.style.configure('W.TButton', foreground='blue')
if __name__ == "__main__":
app = App()
app.mainloop()
All of the buttons are mapped to a "W.TButton" style so when we change that style, all of the widgets associated with that style will change their appearance.

How do I change the size of a tkinter canvas through a new window?

So I have one Tkinter screen that has a canvas. I want to change the size of the canvas by creating a new window that has entry widgets. So I created a new screen and added 2 entry widgets. I want to get the value from those widgets and based on that...it should change the size of the canvas. I tried to do this for an hour, but no luck. Please assist me.
Here is my code
from tkinter import *
# create root window
root = Tk()
# Create Canvas
canvas = Canvas(root, width=50, height=50)
# Create an additional window (the one that is used to enter the new geometry)
dialog = Toplevel(root)
# Add entry widgets for width and height to the new window
width_entry = tk.Entry(dialog)
height_entry = tk.Entry(dialog)
# Add a button to the new window that applies the given width and height
apply_button = Button(dialog, text = 'Apply geometry', command = lambda: canvas.geometry(width_entry.get()+'x'+height_entry.get()))
# Its not possible to get the geometry of a canvas in tkinter...so how do I change the size.
# display the entry boxes and button
width_entry.pack()
height_entry.pack()
apply_button.pack()
# start the tk mainloop
root.mainloop()
Please Assist me
The command you are looking for is canvas.config
Here, I have adjusted the given code:
import tkinter as tk
# create root window
root = tk.Tk()
# Create Canvas
canvas = tk.Canvas(root, width=50, height=50)
canvas.pack()
# Create an additional window (the one that is used to enter the new geometry)
dialog = tk.Toplevel(root)
# Add entry widgets for width and height to the new window
width_entry = tk.Entry(dialog)
height_entry = tk.Entry(dialog)
# Add a button to the new window that applies the given width and height
apply_button = tk.Button(dialog, text = 'Apply geometry', command = lambda: canvas.config(width=width_entry.get(), height=height_entry.get()))
# display the entry boxes and button
width_entry.pack()
height_entry.pack()
apply_button.pack()
# start the tk mainloop
root.mainloop()
I also changed a couple other things:
You imported * from tkinter, but for some items you still led with tk.; I changed them all to match that and switched the import to match as well. (You could still use *, but then just don't have the leading tk.s.)
The canvas was never packed so you could never see what was going on there.
One more suggestion, that line where you make the button is really long. Maybe make a function that does what the lambda does and assign its command to that function instead of a lambda. You can probably see that a line that long is even hard to read here much less if someone (maybe a future version of yourself) was to try to read your code, and edit it or make sense of it. Generally, try to keep all lines down to 80 characters.
Let us know if you have any more questions etc.

How to keep a button stuck at the bottom of a tkniter window?

I am trying to create a tkinter window with a button that is stuck at the bottom. I want the button to be used to add new items to the window (separate code, not shown here), but always keep the button at the bottom. Something like the following:
import tkinter as tk
m = tk.Tk(className="My window")
create = tk.Button(m, text="Create new item", width=25)
create.grid(row=inf, padx=40, pady=20)
m.mainloop()
except of course tkinter.grid() doesn;t accept inf as a valid value for row=. My question is how can I ensure that even as I add items to the tkinter window, my button will always remain on the bottom.
You can combine pack and grid.
Your base application could be this
upperframe.pack(side=tk.TOP)
bottomframe.pack(side=tk.BOTTOM)
Within the upperframe use your grid to add buttons and whatever. The bottom frame containing your changing buttons will be stuck there. You can use pack or grid or whatever you like there.
Creating a status bar goes much the same way.

Detect click on border of widget in tkinter 3

I am trying to make a kind of movable widget program in python with Tkinter, but I ran into a problem. I can't detect a click without interfering with the function of the widget that you click on. (example: Text or Button widget)
Here is an example:
import tkinter as tk
main = tk.Tk()
notes = tk.Text(main, height = 15, bd = 4)
notes.place(y = 10, x = 20)
notes.bind("<Button-1>", lambda event: print("hello"))
But if you try and click in the middle, it still works. Is there any way to make it only clickable on the border and not the widget itself?
You can prevent the default behavior by returning "break" from the event handler. For example:
def hello(event):
print("hello")
return "break"
...
notes.bind("<Button-1>", hello)
The same works for any widget. This prevents the default behavior (moving the cursor, clicking the button) from happening.
Another choice is to put each widget in a frame with a small border, and then put the binding on the frame.

Categories