Grey border around tkinter widgets (not using canvas) - python

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 *

Related

Tkinter: Remove active button highlight

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.

How to configure map for all ttk widgets except TButton?

I am creating a GUI with Tkinter and ttk, and I'm trying to create a custom map to make ttk widgets blue on hover. I could apply that to all widgets with passing "." as the first argument to ttk.Style().map(...).
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
style = ttk.Style()
style.map(".", background=[("active", "blue")])
button = ttk.Button(root, text="An example button")
button.pack()
scrollbar = ttk.Scrollbar(root)
scrollbar.pack()
root.mainloop()
But now I want to exclude TButton from this query. That is, I need to make all widgets but TButton blue on hover. How can I do that?
Passing ".!TButton" as well as "!TButton" instead of "." has no effect.
There is a root style whose name is '.'. To change some feature's default appearance for every widget, you can configure this style. For example, let's suppose that you want all text to be 12-point Helvetica (unless overriden by another style or font option).
So we can override it by simply adding another style:
style.map('TButton',background=[('active','#f0f0f0')])
Keep a note that you might want to do this for every style that you wish to set to default too.
Take a read here for more info.

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

How can I connect a StringVar to a Text widget in Python/Tkinter?

Basically, I want the body of a Text widget to change when a StringVar does.
Short version is, you can't. At least, not without doing extra work. The text widget doesn't directly support a variable option.
If you want to do all the work yourself it's possible to set up a trace on a variable so that it keeps the text widget up to date, and you can add bindings to the text widget to keep the variable up to date, but there's nothing built directly into Tkinter to do that automatically.
The main reason this isn't directly supported is that the text widget can have more than just ascii text -- it can have different fonts and colors, embedded widgets and images, and tags.
I thought of this while working on a project. It's actually really simple:
import tkinter as tk
from tkinter import *
from tkinter.scrolledtext import ScrolledText
def get_stringvar(event):
SV.set(ST1.get("1.0", END))
ST2.replace("1.0", END, SV.get())
root = tk.Tk()
SV = StringVar()
ST1 = ScrolledText(root)
ST1.pack()
ST1.bind('<KeyRelease>', get_stringvar)
ST2 = ScrolledText(root)
ST2.pack()
root.mainloop()

Categories