简体   繁体   English

如何更改按钮边框的颜色 tkinter

[英]How do i change the colour of a button border tkinter

How do i change the colour of a border in tkinter如何更改 tkinter 中边框的颜色

I have looked at other solutions which recommended using highlightcolor and highlightbackground , however these did not work.我查看了其他建议使用highlightcolorhighlightbackground的解决方案,但是这些都不起作用。

excercises_button = Button(canvas, width=327, height=150, image=dumbell_img,borderwidth=4, relief="ridge", bg = "gray55", command = Excercises)
canvas_excercises_button = canvas.create_window(168, 724, window=excercises_button)

I would like the border of this button to be orange.我希望这个按钮的边框是橙色的。

This is what it currently looks like: https://i.stack.imgur.com/3QX8X.png这是它目前的样子: https://i.stack.imgur.com/3QX8X.png

Here is an example of how one can have a kind of border created by using a frame and a button. 这是一个示例,说明如何使用框架和按钮创建一种边框。

import tkinter as tk

root = tk.Tk()

frame = tk.Frame(root, highlightbackground="orange", highlightcolor="orange", highlightthickness=4, bd=0)
frame.grid(row=0, column=0)
# adding weights so the button is center on the frame.
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)

btn = tk.Button(frame,text="test", borderwidth=4, relief="ridge", bg = "gray55").grid(row=0, column=0)
root.mainloop()

Results: 结果:

在此处输入图片说明

I found a solution! 我找到了解决方案!

What i did is I created a rectangle which surrounded the buttons. 我所做的是我创建了一个包围按钮的矩形。

canvas.create_rectangle(0, 638, 1100, 900, fill=colour)

I then made the three buttons a few pixels smaller to allow the background of the rectangle to show. 然后,我将三个按钮缩小了几个像素,以显示矩形的背景。

Since this is the top answer on a google search, i will share the method found on the duplicate of this question :由于这是谷歌搜索的最佳答案,我将分享在这个问题的副本中找到的方法:
(code refactored to work with python 3.10) (重构代码以使用 python 3.10)

from tkinter import ttk,Tk
root = Tk()

style = ttk.Style(root)
style.theme_use('clam')
style.configure('my.TButton', bordercolor="red")

ttk_button = ttk.Button(root, text='Button', style='my.TButton')
ttk_button.pack()

root.mainloop()

Resulting window结果 window
this code works because certain ttk themes allow changing the border color on buttons.此代码有效,因为某些 ttk 主题允许更改按钮上的边框颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM