英文:
How to create a transparent label inside a progress bar with custom tkinter
问题
我正在尝试在进度条内创建一个标签,同时仍然显示底部的进度小部件。
这是我的代码:
import customtkinter as ctk
window = ctk.CTk()
prog_num = ctk.IntVar(value=100)
prog_str = ctk.StringVar(value='100/100')
prog_frm = ctk.CTkFrame(window)
prog_pbr = ctk.CTkProgressBar(prog_frm,
variable=prog_num,
height=15,
progress_color='#fa2844')
prog_lbl = ctk.CTkLabel(prog_pbr, textvariable=prog_str, fg_color='transparent')
prog_pbr.pack(fill='x')
prog_lbl.place(rely=0.5, relx=0, anchor='w')
prog_frm.pack(fill='x')
window.mainloop()
尽管fg_color设置为transparent,但仍然显示某种标签背景。我找到的唯一解决方案仅适用于常规的tkinter。
英文:
I'm trying to create a label inside a progress bar while still showing the progress widget underneath
here is my code:
import customtkinter as ctk
window = ctk.CTk()
prog_num = ctk.IntVar(value=100)
prog_str = ctk.StringVar(value='100/100')
prog_frm = ctk.CTkFrame(window)
prog_pbr = ctk.CTkProgressBar(prog_frm,
variable=prog_num,
height=15,
progress_color='#fa2844')
prog_lbl = ctk.CTkLabel(prog_pbr, textvariable=prog_str, fg_color='transparent')
prog_pbr.pack(fill='x')
prog_lbl.place(rely=0.5, relx=0, anchor='w')
prog_frm.pack(fill='x')
window.mainloop()
even thought he fg_color is set to transparent it still shows some sort of label background
The only solutions ive found only work for regular tkinter
答案1
得分: 1
如果您研究customtkinter
的代码,您会发现CTkProgressBar
是一个Frame
小部件内的Canvas
小部件。因此,您可以使用Canvas.create_text()
在Canvas
小部件内绘制所需的透明文本。
以下是使用自定义的CTkProgressBar
的示例,该示例在进度条的中心显示进度文本:
import customtkinter as ctk
ctk.set_appearance_mode("dark")
class MyProgressBar(ctk.CTkProgressBar):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# 在内部画布中创建文本项
self._canvas.create_text(0, 0, text=self._variable.get(), fill="yellow",
font=('Arial', 10), anchor="c", tags="progress_text")
# 覆盖函数以将进度文本移动到内部画布的中心
def _update_dimensions_event(self, event):
super()._update_dimensions_event(event)
self._canvas.coords("progress_text", event.width/2, event.height/2)
# 覆盖函数以在设置新值时更新进度文本
def set(self, val, **kwargs):
super().set(val, **kwargs)
self._canvas.itemconfigure("progress_text", text=int(val*100))
window = ctk.CTk()
prog_frm = ctk.CTkFrame(window)
prog_num = ctk.IntVar(value=50)
# 创建自定义进度条
prog_pbr = MyProgressBar(prog_frm,
variable=prog_num,
height=25,
progress_color='#fa2844')
prog_pbr.pack(fill='x', padx=50, pady=20)
prog_frm.pack(fill='x')
# 循环更新进度条以进行演示
def update(val=0.0):
prog_pbr.set(val)
window.after(100, update, (val+0.01)%1)
update()
window.mainloop()
和输出:
英文:
If you study the code of customtkinter
, you will find that CTkProgressBar
is a Canvas
widget inside a Frame
widget. So you can draw the required transparent text inside the Canvas
widget using Canvas.create_text()
.
Below is an example using a custom CTkProgressBar
that show the progress text at the center of the bar:
import customtkinter as ctk
ctk.set_appearance_mode("dark")
class MyProgressBar(ctk.CTkProgressBar):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# create the text item in the internal canvas
self._canvas.create_text(0, 0, text=self._variable.get(), fill="yellow",
font=('Arial', 10), anchor="c", tags="progress_text")
# override function to move the progress text at the center of the internal canvas
def _update_dimensions_event(self, event):
super()._update_dimensions_event(event)
self._canvas.coords("progress_text", event.width/2, event.height/2)
# override function to update the progress text whenever new value is set
def set(self, val, **kwargs):
super().set(val, **kwargs)
self._canvas.itemconfigure("progress_text", text=int(val*100))
window = ctk.CTk()
prog_frm = ctk.CTkFrame(window)
prog_num = ctk.IntVar(value=50)
# create the custom progress bar
prog_pbr = MyProgressBar(prog_frm,
variable=prog_num,
height=25,
progress_color='#fa2844')
prog_pbr.pack(fill='x', padx=50, pady=20)
prog_frm.pack(fill='x')
# loop to update the bar for demonstration
def update(val=0.0):
prog_pbr.set(val)
window.after(100, update, (val+0.01)%1)
update()
window.mainloop()
And the output:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论