Changing the colour of button border with python tkinter - python

I'm trying to change the color of my border on this button however it does nothing or gives me a grey border.
self._lqbutton = tk.Button(self._longquestionframe,
text="TEST",
bg="blue", fg="#fff",
highlightbackground="red",
highlightcolor="red",
highlightthickness=4,
relief=tk.SOLID,
borderwidth="4")
self._lqbutton.pack()
I have also tried using relief=tk.FLAT

Managed to achieve the effect by placing it inside its own frame and changing the frame attributes.
self._lqbuttonborder = tk.Frame(self._longquestionframe,
highlightbackground="#bce8f1",
highlightcolor="#bce8f1",
highlightthickness=1,
bd=0)

You cannot change the border color of a widget. The highlightcolor and highlightbackground attributes changes the color of the highlight ring, which is the border-like decoration that appears only when the widget has focus.

Related

tkinter combobox border style is not working anymore

I have a tkinter app that has multiple combo boxes and at the bottom also a Text widget. In the beginning, I used the style method in order to change the Combobox look but then I had to use a canvas in order to put text over the picture background and for some reason, my comboxes are now not changing their style anymore and are staying plain. here is a part of my code with only one combo box since the whole code is long but the concept i think should be applied the same:
roo = Tk()
roo.title('Shift Request')
roo.geometry('600x450')
dir_path_main_bg = os.path.join(dir_path, r"JUSTT BG For Main Window.png")
picture = PhotoImage(file=dir_path_main_bg, master=roo)
bg = Label(roo, image=picture)
bg.place(x=0, y=0, relwidth=1, relheight=1)
# Changing the combobox menu style.
roo.option_add('*TCombobox*Listbox.selectBackground', '#30D5C8') # change highlight color
roo.option_add('*TCombobox*Listbox.selectForeground', 'white') # change text color
style = ttk.Style()
style.configure('TCombobox', background='#30D5C8') # Create a border around the combobox button.
style.map('TCombobox', foreground=[('hover', 'black')], background=[('hover', '#30D5C8')]) # style the combobox when
# it's beeing hovered on.
# Creating a canvas for the main window menu to choose your shifts.
main_menu_canvas = Canvas(roo, width=500, height=450, highlightbackground='#30D5C8', highlightcolor='#30D5C8')
main_menu_canvas.pack(fill='both', expand=True)
main_menu_canvas.create_image(0, 0, image=picture, anchor='nw')
# A function to stop the highlighting when choosing a value.
def defocus(event):
event.widget.master.focus_set()
# Second drop-down menu.
chosen2 = tk.StringVar(value='Shift Starting Hour')
vyber2 = ttk.Combobox(roo, textvariable=chosen2, state='readonly')
vyber2['values'] = start_shift
vyber2.bind("<FocusIn>", defocus)
vyber2.place(relx=0.27, rely=0.16, relwidth=0.45, relheight=0.09)
vyber2.config(font=('calibri', '13'))
# Changing the combobox menu style.
roo.option_add('*TCombobox*Listbox.selectBackground', '#30D5C8') # change highlight color
roo.option_add('*TCombobox*Listbox.selectForeground', 'white') # change text color
style = ttk.Style()
style.configure('TCombobox', background='#30D5C8') # Create a border around the combobox button.
style.map('TCombobox', foreground=[('hover', 'black')], background=[('hover', '#30D5C8')]) # style the combobox when
# # it's beeing hovered on.
# Creating the text box to write notes.
default_val = tk.StringVar(value='Notes...').get()
text_box = Text(main_menu_canvas, width=45, height=6, font=('calibri','13'))
text_box.insert(INSERT, default_val)
text_box.config(highlightthickness=0.5, highlightbackground='#30D5C8', highlightcolor='#30D5C8')
main_menu_canvas.create_window(300, 336, window=text_box)
roo.mainloop()
My desired result is to make it look like how it was before. The first screenshot is the way it looks now and the second screenshot it's how it was before the text addition.
Would appreciate any help.

How do I move the bottom label to the extreme right in Tkinter?

Here's my code snippet
footer_frame = tk.Frame()
tk.Label(footer_frame, text = 'by .....', font = 'arial 10', padx=5, pady=5).pack(side = tk.RIGHT, anchor='se')
footer_frame.pack(tk.BOTTOM)
To put the label all the way to the right inside the footer, the simplest way is to use pack and telling it to be on the right side.
Since you didn't have footer_frame fill the bottom, it's going to be centered by default. And since a frame shrinks to fit its contents, the label will also appear centered.
Here's an example, which uses side='right' for the label, and fill='x' for the footer frame. I gave the label a distinctive color so that you can see where it is in relation to the window and frame.
import tkinter as tk
root = tk.Tk()
root.geometry("400x200")
footer_frame = tk.Frame(root)
label = tk.Label(footer_frame, text="by .....", background="bisque")
footer_frame.pack(side="bottom", fill="x")
label.pack(side="right")
root.mainloop()

How do I change the color of a button in Python's Tkinter

I was writing a gui and I wanted to change the color of a button but background seems to change the color of an outline instead of the full background. How do I change the color of the background of a button?
I have tried the background and style.
salmon = "#FFC6AB"
black = "#0C120C"
# ADDING BUTTONS
# Adding style
style = ttk.Style()
style.configure("X.TFrame", background=grey)
style. configure("X.TButton", background=salmon, foreground=black, font=("Courier", 20), width=17)
# Adding spacer
spacer_1 = ttk.Frame(root)
spacer_1.grid(column=0, row=1, pady=12)
spacer_1.configure(style="X.TFrame")
# Adding frame
frame_2 = ttk.Frame(root)
frame_2.grid(column=0, row=2)
frame_2.configure(style="X.TFrame")
# Adding button 1
button_1 = ttk.Button(frame_2, text="Scale Cookbook")
button_1.grid(column=0, row=0)
button_1.configure(style="X.TButton")
You are doing it the right way, your code does change the button color in Linux. You don't get the result you want because the default ttk themes for Windows and Mac do not allow to change the button background color (I think because they are created from images).
If you want to change the button background color, you can change theme for one that allows it, like 'clam' or 'alt':
style = ttk.Style()
style.theme_use('clam')
# ... the rest of your style configuration

Adding widgets over canvas in tkinter

I want to put a small image and other widgets over a canvas on which an image is displayed. I've tried options such ascompound and other things.
Background picture is fine and the small image that I want to put over the background image shows fine but it's always top or bottom of the window. I want it to be placed over any area of background image. I've tried many options of all the geometry manager (pack, grid, place) but none of them works. Please help, here's my code :
from Tkinter import *
root = Tk()
root.iconbitmap('E:/a.ico')
root.title('Unick Locker')
canvas = Canvas(root, width=730, height=600)
canvas.grid()
bgImg = PhotoImage(file="E:/a.gif")
canvas.create_image(370, 330, image=bgImg)
login = PhotoImage(file="E:/login.gif")
lo = Label(root, image=login)
lo.grid()
root.mainloop()
In order to add any widgets over or the foreground of any background image or canvas, the row and column values of all the widgets must be same as of the background image. so, my above mentioned program would be like this :
from Tkinter import *
root = Tk()
root.iconbitmap('E:/a.ico')
root.title('Unick Locker')
canvas = Canvas(root, width=730, height=600)
canvas.grid(row=0, column=0)
bgImg = PhotoImage(file="E:/a.gif")
canvas.create_image(370, 330, image=bgImg)
login = PhotoImage(file="E:/login.gif")
lo = Label(root, image=login)
lo.grid(row=0, column=0)
root.mainloop()
I tried putting the same row and column values to the widgets in grid() methods which I wanted to put over the image, and it worked fine as I wanted :-)
Have you considered using the paste method, which lets you define the position of the pasted image through a box argument?
See http://effbot.org/imagingbook/imagetk.htm.
Please also take a look at this thread: Tkinter, overlay foreground image on top of a background image with transparency, which seems very similar to your issue.
You are looking to draw the widgets over the canvas, this means you must specify the canvas as the parent widget, not the root as you did. For that, modify lo = Label(root, image=login) to lo = Label(canvas, image=login)
Also, do not forget to specify the rows and columns where you want to position the different widgets. This means you need to write, for example, lo.grid(row=0, column=0) instead of lo.grid(). For the moment you do not see big problems because you have only one label widget. But if you try to add an other widget without mentioning the exact positions (rows and columns) you will get unexpected results.
This question isn't about images at all, it's just a basic layout problem. You'll have the same issues with or without images. The problem is simply that you aren't giving any options to grid, so it naturally puts things at the top. Tkinter also has the behavior that a containing widget (eg: your canvas) will shrink or expand to exactly fit its contents.
Here's a version that creates several widgets over a background image. Notice the use of options to pack and grid, and the use of grid_rowconfigure and grid_columnconfigure to specify how extra space is allocated.
from Tkinter import *
root = Tk()
canvas = Canvas(root, width=730, height=600)
canvas.pack(fill="both", expand=True)
bgImg = PhotoImage(file="E:/a.gif")
canvas.create_image(370, 330, image=bgImg)
l1 = Label(canvas, text="Hello, world")
e1 = Entry(canvas)
t1 = Text(canvas)
l1.grid(row=0, column=0, sticky="ew", padx=10)
e1.grid(row=1, column=1, sticky="ew")
t1.grid(row=2, column=2, sticky="nsew")
canvas.grid_rowconfigure(2, weight=1)
canvas.grid_columnconfigure(2, weight=1)
root.mainloop()

Creating Frame inside a Canvas

I am trying to make a frame scrollable, and the only way I found to do this is making a scrollable canvas and adding a frame to it. This would work fine, if it worked for me.
I am able to create a scrollable canvas that works fine, but I can't seem to properly add a frame inside of it:
self.title = Label(root, text="Brnr", font=("Helvetica", 50), anchor = W, pady = 40, padx = 50)
self.title.pack (anchor = NW)
#creates title widget for title
self.frame = Frame(screen, bd =1)
self.frame.pack(fill = BOTH)
#Creates frame widget under which all other widgets will be kept
self.canvas = Canvas(self.frame, bd=1,scrollregion=(0,0, 1000, 1000), height = 600)
#creates canvas so that screen can be scrollable
self.scrollbar = Scrollbar(self.frame, command=self.canvas.yview)
#creates scrollbar
self.canvas.config(yscrollcommand=self.scrollbar.set)
#connects the scrollbar to the canvas
self.scrollbar.pack(side=RIGHT, fill=Y)
self.canvas.pack(expand=YES, fill=BOTH)
#packs the scrollbar and canvas so that they fill the remainder of the screen
self.frameC = Frame(bg = "red")
self.canvas.create_window(0,0, anchor = NW, window = self.frameC, width = 200, height = 200)
#creates window on the scrollable area to add other widgets
self.frameC.pack()
self.groupRec = LabelFrame(self.frameC, text ="Recommendations:", font=("Helvetica", 20))
self.groupRec.pack()
self.signupButton = Button(self.groupRec, text="Sign Up", width=10)
self.signupButton.pack(side=RIGHT)
#creates button to submit login
This gives me a scrollable, but empty, canvas, with none of the labelframe/button appearing.
By default, when you add a window to a canvas, the center of the window will be at the coordinates you give. Thus, the center of your frame will be at 0,0 which is the upper-left corner of the canvas. You can't see the widgets because they are outside the borders of the canvas.
The solution is to include anchor="nw" in the call to create_window, which will place the upper-left corner of your frame in the upper left corner of your canvas.
Don't forget to set the scroll region of the canvas to match the size of your frame. The easiest way to do that is with the command self.canvas.config(scrollregion=self.canvas.bbox("all")). You'll probably also need to add a binding to <Configure> on the canvas so that you can resize the inner frame when the user resizes the window. That's not always necessary, it depends a bit on exactly what you are trying to accomplish.
Here's a pro tip: to debug problems like this it's really helpful to temporarily give your frame and canvas different colors to more easily visualize what is happening.
Don't re-invent the wheel. Install Pmw (Python meta-widgets), assuming you are using Tkinter, http://pmw.sourceforge.net/ and use Pmw.ScrolledFrame.

Categories