![](/img/trans.png)
[英]How do I change background colour of a button on hover in Tkinter?
[英]How do i change the colour of a button border tkinter
如何更改 tkinter 中邊框的顏色
我查看了其他建議使用highlightcolor
和highlightbackground
的解決方案,但是這些都不起作用。
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)
我希望這個按鈕的邊框是橙色的。
這是它目前的樣子: https://i.stack.imgur.com/3QX8X.png
這是一個示例,說明如何使用框架和按鈕創建一種邊框。
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()
結果:
我找到了解決方案!
我所做的是我創建了一個包圍按鈕的矩形。
canvas.create_rectangle(0, 638, 1100, 900, fill=colour)
然后,我將三個按鈕縮小了幾個像素,以顯示矩形的背景。
由於這是谷歌搜索的最佳答案,我將分享在這個問題的副本中找到的方法:
(重構代碼以使用 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()
結果 window
此代碼有效,因為某些 ttk 主題允許更改按鈕上的邊框顏色。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.