I am trying to change to border color of a button in python tkinter, I've tried putting it in a frame but it does not work:
import tkinter
from tkinter import *
root = Tk()
l_f = Frame(root, bg="black")
l_f.pack()
l = Button(l_f, text="fdffdfd", bg="white", fg="white")
l.pack()
root.mainloop()
You packed a frame
, but didn't set any option of that frame, to give a border effect.
Try: l_f = Frame(root, highlightbackground = "black", highlightthickness = 2, bd=0)
Also, you can use LabelFrame
in place of frame, to add border to a button.
l_f = LabelFrame(root, bg = "black", bd=2)
Making a button relief have a different color to the button face and have it work like button is not trivial.
Here is a working example of a button with color that performs just like a standard button.
I've changed the way Button key press and release works, in order to match button behaviour.
import tkinter as tk
class color_button:
def virtual(self, o, n, f, args):
'''virtual(o, n, f, args)'''
o.event_add(n, *args)
b = o.bind(n, f)
return b
def flexx(self, o, r = 0, c = 0, rw = 1, cw = 1) ->'Set grid manager to grow':
'''flexx(o(TkContainer), r=0, c=0, rw=1, cw=1)'''
if r != None:
o.rowconfigure(r, weight = rw)
if c != None:
o.columnconfigure(c, weight = cw)
def closer(self, ev):
self.master.destroy()
def __init__(self, color = "red"):
self.master = tk.Tk()
self.flexx(self.master)
self.frame = tk.LabelFrame(
self.master, bg = color, bd = 2, relief = "raised")
self.frame.grid row = 0, column = 0, sticky = "nsew")
self.flexx(self.frame)
self.button = tk.Button(
self.frame, text = "button", relief = "flat",
bd = 0, command = self.action)
self.button.grid(row = 0, column = 0, sticky = "nsew")
self.virtual(
self.button, "<<PRESS>>",
self.press, ( "<ButtonPress-1>", "<KeyPress-space>" ))
self.virtual(
self.button, "<<RELEASE>>",
self.release, ( "<ButtonRelease-1>", "<KeyRelease-space>" ))
self.master.bind("<Escape>", self.closer)
def press(self, ev):
self.frame["relief"] = "sunken"
return "break"
def release(self, ev):
self.frame["relief"] = "raised"
wd = ev.widget
if ev.num != 1 or wd.winfo_containing( *wd.winfo_pointerxy( )) == wd:
wd.invoke( )
return "break"
def action(self):
print( "Done" )
if __name__ == "__main__":
main = color_button( )
main.master.mainloop()
I am trying to change to border color of a button in python tkinter, I've tried putting it in a frame but it does not work:
import tkinter
from tkinter import *
root = Tk()
l_f = Frame(root, bg="black")
l_f.pack()
l = Button(l_f, text="fdffdfd", bg="white", fg="white")
l.pack()
root.mainloop()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.