使用Tkinter创建圆形进度条?

4
我想在我的Python GUI中使用Tkinter添加一个圆形进度条,但我没有找到关于使用Tkinter创建圆形进度条的文档。
如何在Tkinter中创建一个圆形进度条?或者这不可能吗?

1
据我所知,没有内置的功能。您应该能够创建自己的功能,例如在 Canvas 对象上绘制弧线和/或椭圆。 - martineau
4个回答

10

虽然没有内置的功能,但您可以创建自己的函数。以下是一个手动在Canvas上绘制和更新图形的示例:

try:
    import Tkinter as tk
    import tkFont
    import ttk
except ImportError:  # Python 3
    import tkinter as tk
    import tkinter.font as tkFont
    import tkinter.ttk as ttk


class CircularProgressbar(object):
    def __init__(self, canvas, x0, y0, x1, y1, width=2, start_ang=0, full_extent=360.):
        self.custom_font = tkFont.Font(family="Helvetica", size=12, weight='bold')
        self.canvas = canvas
        self.x0, self.y0, self.x1, self.y1 = x0+width, y0+width, x1-width, y1-width
        self.tx, self.ty = (x1-x0) / 2, (y1-y0) / 2
        self.width = width
        self.start_ang, self.full_extent = start_ang, full_extent
        # draw static bar outline
        w2 = width / 2
        self.oval_id1 = self.canvas.create_oval(self.x0-w2, self.y0-w2,
                                                self.x1+w2, self.y1+w2)
        self.oval_id2 = self.canvas.create_oval(self.x0+w2, self.y0+w2,
                                                self.x1-w2, self.y1-w2)
        self.running = False

    def start(self, interval=100):
        self.interval = interval  # Msec delay between updates.
        self.increment = self.full_extent / interval
        self.extent = 0
        self.arc_id = self.canvas.create_arc(self.x0, self.y0, self.x1, self.y1,
                                             start=self.start_ang, extent=self.extent,
                                             width=self.width, style='arc')
        percent = '0%'
        self.label_id = self.canvas.create_text(self.tx, self.ty, text=percent,
                                                font=self.custom_font)
        self.running = True
        self.canvas.after(interval, self.step, self.increment)

    def step(self, delta):
        """Increment extent and update arc and label displaying how much completed."""
        if self.running:
            self.extent = (self.extent + delta) % 360
            self.canvas.itemconfigure(self.arc_id, extent=self.extent)
            # Update percentage value displayed.
            percent = '{:.0f}%'.format(
                                    round(float(self.extent) / self.full_extent * 100))
            self.canvas.itemconfigure(self.label_id, text=percent)
        self.canvas.after(self.interval, self.step, delta)


    def toggle_pause(self):
        self.running = not self.running


class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.grid()
        self.createWidgets()

    def createWidgets(self):
        self.canvas = tk.Canvas(self, width=200, height=200, bg='white')
        self.canvas.grid(row=0, column=0, columnspan=2)

        self.progressbar = CircularProgressbar(self.canvas, 0, 0, 200, 200, 20)

        self.pauseButton = tk.Button(self, text='Pause', command=self.pause)
        self.pauseButton.grid(row=1, column=0)
        self.quitButton = tk.Button(self, text='Quit', command=self.quit)
        self.quitButton.grid(row=1, column=1)

    def start(self):
        self.progressbar.start()
        self.mainloop()

    def pause(self):
        self.progressbar.toggle_pause()

if __name__ == '__main__':
    app = Application()
    app.master.title('Sample application')
    app.start()

以下是它运行时的一些截图:

it运行时的截图


好的,我会尝试将其用于带有尾随彗星轨迹的无限1秒循环中。在等待谷歌Python API响应电子邮件消息时需要它。如果您已经编写了,请提供链接。 - WinEunuuchs2Unix
谢谢。不好意思,我没有写过那样的版本。请注意,我只是稍微修改了这个答案。 - martineau

3

我没有足够的声望添加评论,但是目前马丁诺提供的代码不能正常工作,你需要进行更改。

    if self.running:
        self.cur_extent = (self.cur_extent + delta) % 360
        self.canvas.itemconfigure(self.arc_id, extent=self.cur_extent)

to

    if self.running:
        self.extent = (self.extent + delta) % 360
        self.cur_extent = (self.extent + delta) % 360
        self.canvas.itemconfigure(self.arc_id, extent=self.cur_extent)

1

Tkinter没有对圆形进度条提供任何支持。您需要使用一系列图像或在画布上绘制自己的图案。


我也这么想。无论如何还是谢谢你。 - Atalanttore

0

此外,@martineau的代码需要进行修改

self.tx, self.ty = (x1-x0) / 2, (y1-y0) / 2

需要更改为

self.tx, self.ty = (x1+x0) / 2, (y1+y0) / 2

这样标签的ID实际上就在中间了


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接