Tkinter: Remove active button highlight - python

How can I remove the dotted black border after a button is clicked in Tkinter (with Ttkthemes)?
I'm on Windows 10, Python 3.7.9.
There seems to be no uniform way to remove it and I searched across Google and SO with no luck. Thanks.
Here's a minimal example:
import tkinter
import tkinter.ttk
from ttkthemes import ThemedTk
tk = ThemedTk(theme="arc")
tk.configure(background="#f5f6f7")
tk.resizable(0,0)
selectFileInput = tkinter.ttk.Button(
tk,
text="Select Input File"
)
selectFileInput.place(x=20,y=60)
tk.mainloop()

I found the solution. It was to create a dummy button and focus it to remove focus from the button to the dummy button by using dummy.focus()

ttk.Button has the keyword argument takefocus this can be set to false and the button does not take focus after it is clicked.
ttk.Button(.., .., takefocus=False)
Therefore you do not need a hack with a dummy button that takes focus as in the answer.

Related

Tkinter widgets only drawn if I press alt button

I have a small app on Linux in which I sub-class the Frame class from tkinter and sometime the widgets are only drawn after I press the alt button. Everything works fine, though this is the first time I use this GUI and I may miss something.
Follows a short example of my code:
from tkinter import *
class MyFrame(Frame):
def one_method(self):
root_frame.geometry("200x400")
button = Button(self, command=self.clean_and_draw_more_widget, text="Hello")
button.pack(side=TOP) #I also use grid()
the main driver code:
root_frame = Tk()
root_frame.title("Window")
frame = MyFrame(root_frame)
frame.pack()
root_frame.mainloop()
As you can see nothing compless, but still I have this problem.
EDIT
I use grid() and pack() but each one in different methods
Thanks for helping

Button momentarily changes color when selected tkinter

Is there a best practice for making a tkinter button momentarily change color when it is selected (so the user gets a visual feedback that the button was pressed).
I have read it is not a good idea to use time.sleep() in a tkinter GUI.
When my button is pressed, the code happens so fast that even when I have a button.config() command to change color, it doesn't occur without using time.sleep()
Any suggestions?
I think this may be what you want:
Button(background=normal_color, foreground=text_color,
activebackground=pressed_color, activeforeground=pressed_text_color)
This changes the button from normal_color to pressed_color when the button is pressed.
It's actually a simple question with a simple answer but I had to look everywhere too. Finally found this answer by reading http://effbot.org/tkinterbook/button.htm.
You can change the color upon clicking, then use the after method to reset the color back to the original, after some time has passed
import tkinter as tk
def reset_color():
bt['fg'] = 'black'
def clickme():
print('clicked')
bt['fg'] = 'red'
root.after(2000, reset_color) # after 2 seconds
root = tk.Tk()
bt = tk.Button(root, text='will color for a while\nafter clicked', command=clickme)
bt.pack()
root.mainloop()

Grey border around tkinter widgets (not using canvas)

I am experiencing issues with a Python 3 tkinter program I'm working on.
I am running macOS Sierra.
When running the app, every widget has a grey border around it.
Is there any way to remove this?
Screenshot of the border:
Here's the code:
# Item list
itemlist=Treeview(root)
itemlist.heading("#0", text="Item Name")
itemlist["columns"]=("1")
itemlist.column("1",width=50)
itemlist.heading("1",text="Item ID")
itemlist.bind("<Double-1>", select)
itemlist.grid(row=2,column=1,padx=10,pady=10)
# Nametag
Label(root,text="Name:").grid(row=3,column=0)
# 'Save' Button
saveButton=Button(text="Save")
saveButton.bind("<Button-1>",savebind)
saveButton.grid(row=1,column=0)
# 'Add New' button
newItemButton=Button(text="New Event")
newItemButton.bind("<Button-1>",newItem)
newItemButton.grid(row=0,column=1)
# Name entry text field
itemNameEntry=Entry(root,width=25)
itemNameEntry.grid(row=3,column=1)
# Submit Button
submitButton=Button(root,width=25,text="Submit")
submitButton.grid(row=4,column=1)
submitButton.bind("<Button-1>",submit)
# Begin loading
load()
# Start GUI
root.mainloop()
Configure your widgets to use highlightbackground = 'white' (or whatever your background colour is) and set your highlightthickness=0
This should remove the grey outline.
EG
itemNameEntry=Entry(root,width=25, highlightbackground='white')
itemNameEntry.config(highlightthickness=0)
You can use widget.config(highlightthickness=0) or pass this parameter in widget constructor.
If you are importing tkinter.ttk you need to import that first, then import tkinter.
from tkinter.ttk import *
from tkinter import *

Tkinter custom create buttons

Can tkinter create custom buttons from an image or icon like this?
It's possible!
If you check out the button documentation, you can use an image to display on the button.
For example:
from tkinter import *
root = Tk()
button = Button(root, text="Click me!")
img = PhotoImage(file="C:/path to image/example.gif") # make sure to add "/" not "\"
button.config(image=img)
button.pack() # Displaying the button
root.mainloop()
This is a simplified example for adding an image to a button widget, you can make many more cool things with the button widget.
I created a library called CustomTkinter, and with it you can create more or less exactly what is shown in the images above. CustomTkinter provides new widgets for Tkinter, which can be customised in color and shape. Here I tried to create something similar to the image above:
You can find the example code to the above image here.
There is not also a Button, but many other elements, and it also supports a dark and light theme:
You can check out the library here:
https://github.com/TomSchimansky/CustomTkinter
A simple example would be:
import tkinter
import customtkinter
customtkinter.set_appearance_mode("System")
customtkinter.set_default_color_theme("blue")
root_tk = customtkinter.CTk() # create CTk window like the Tk window
root_tk.geometry("400x240")
def button_function():
print("button pressed")
# Use CTkButton instead of tkinter Button
button = customtkinter.CTkButton(master=root_tk, command=button_function)
button.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
root_tk.mainloop()
which gives the following on macOS:

Is there a way to make a Text widget in Tkinter non-clickable and non-editable?

Is it possible do make a Text widget in Tkinter non-clickable and non-editable?
Just set its state to "disabled":
from Tkinter import Tk, Text, DISABLED
r = Tk()
Text(r, state=DISABLED).grid()
r.mainloop()
You can even enter some text before you disable it.
from Tkinter import Tk, Text, DISABLED
r = Tk()
t = Text(r)
t.grid()
t.insert(0.0, "BLAH!")
# Just make sure you disable it AFTER you put the text in
t["state"] = DISABLED
r.mainloop()
Then I suppose you could set its background to be grey or something so that people know it is inactive.
Edit:
Since you asked, you can reactivate the textbox like this:
# Note you have to have NORMAL imported
t["state"] = NORMAL

Categories