如何在自定义的tkinter进度条内创建一个带有透明标签的部分。

huangapple go评论65阅读模式
英文:

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()

code output:

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()

和输出:

如何在自定义的tkinter进度条内创建一个带有透明标签的部分。

英文:

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:

如何在自定义的tkinter进度条内创建一个带有透明标签的部分。

huangapple
  • 本文由 发表于 2023年7月11日 04:25:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定