簡體   English   中英

改變 tkinter 縮放部件的形狀

[英]Changing shapes of tkinter scale widgets

我想改變 tkinter 縮放小部件的形狀。 基本上我想把 slider 和槽都改成不同的形狀。 有誰知道如何做到這一點? 我在網上查了一下,沒有找到我的查詢的任何結果。 作為 tkinter 的新手,請盡量保持簡單。 我發現了一些關於更改按鈕小部件形狀的結果( 看這里),但這似乎對我沒有多大幫助。 我希望我的天平看起來像這張圖片頂部顯示的天平:

所需外觀的示例

比例不必與上圖中的完全一樣。 任何關於如何使我的規模看起來更好的建議將不勝感激。

這是我的代碼的相關部分:

scale1 = ttk.Scale(root,from_=100,to=0,orient=VERTICAL,variable=s_var1)
scale2 = ttk.Scale(root,from_=100,to=0,orient=VERTICAL,variable=s_var2)
scale3 = ttk.Scale(root,from_=100,to=0,orient=VERTICAL,variable=s_var3)
scale4 = ttk.Scale(root,from_=100,to=0,orient=VERTICAL,variable=s_var4)
scale5 = ttk.Scale(root,from_=100,to=0,orient=VERTICAL,variable=s_var5)
#displaying the scale widgets on the screen
scale1.grid(row=0,column=0,padx=40,pady=10)
scale2.grid(row=0,column=1,padx=40,pady=10)
scale3.grid(row=0,column=2,padx=40,pady=10)
scale4.grid(row=0,column=3,padx=40,pady=10)
scale5.grid(row=0,column=4,padx=40,pady=10)
#disabling the scales
scale1.config(state=DISABLED)
scale2.config(state=DISABLED)
scale3.config(state=DISABLED)
scale4.config(state=DISABLED)
scale5.config(state=DISABLED)

我也使用過 tkinter.ttk 模塊,但我也不喜歡它的小部件。

從圖片中,我假設您想顯示一些數量,而不是使用縮放到 select 值。 在這種情況下,我寧願使用Progressbar而不是Scale 這將為您提供與圖片頂部相似的內容,但具有簡單的矩形形狀:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
style = ttk.Style(root)

# configure Progressbar style to be flat and with the colors from the pictures
style.configure('my.Vertical.TProgressbar', background='#f7f4bf', troughcolor='#8a7852',
                pbarrelief='flat', troughrelief='flat')

pbar1 = ttk.Progressbar(root, maximum=100, value=25, orient='vertical', style='my.Vertical.TProgressbar')
pbar2 = ttk.Progressbar(root, maximum=100, value=74, orient='vertical', style='my.Vertical.TProgressbar')
pbar3 = ttk.Progressbar(root, maximum=100, value=10, orient='vertical', style='my.Vertical.TProgressbar')
pbar4 = ttk.Progressbar(root, maximum=100, value=45, orient='vertical', style='my.Vertical.TProgressbar')
pbar5 = ttk.Progressbar(root, maximum=100, value=50, orient='vertical', style='my.Vertical.TProgressbar')

pbar1.grid(row=0, column=0, padx=40, pady=10)
pbar2.grid(row=0, column=1, padx=40, pady=10)
pbar3.grid(row=0, column=2, padx=40, pady=10)
pbar4.grid(row=0, column=3, padx=40, pady=10)
pbar5.grid(row=0, column=4, padx=40, pady=10)

root.configure(bg="#231303")
root.mainloop()

截屏

您可以使用pbar1.configure(value=<new_value>)更改進度條顯示的值。

但是,我不知道如何更改進度條的形狀。 要獲得條的自定義形狀,可以從 Canvas 創建自定義條 class 並使用 PIL 獲得透明形狀。

import tkinter as tk
from tkinter import ttk

from PIL import Image, ImageTk

root = tk.Tk()
style = ttk.Style(root)


class MyBar(tk.Canvas):
    def __init__(self, master, shape, value=0, maximum=100,
                 bg="#231303", trough_color='#8a7852', bar_color='#f7f4bf'):
        # open shape mask with PIL
        im_shape_alpha = Image.open(shape).convert('L')
        # create bar shape image with the choosen backgroound color
        im_shape = Image.new('RGBA', im_shape_alpha.size, bg)
        # apply shape as alpha mask to "cut out" the bar shape
        im_shape.putalpha(im_shape_alpha)
        width, height = im_shape_alpha.size
        # create the canvas
        tk.Canvas.__init__(self, master, bg=trough_color, width=width, height=height, highlightthickness=0)

        self._value = value  # bar value
        self.maximum = maximum  # maximum value

        # bar width and height
        self.height = height
        self.width = width
        
        # create tkinter image for the shape from the PIL Image
        self.img_trough = ImageTk.PhotoImage(im_shape, master=self)
        # create bar to display the value
        self.create_rectangle(0, height, width, height * (1 - value/self.maximum), width=0, fill=bar_color, tags='pbar')
        # display shape on top
        self.create_image(0, 0, anchor='nw', image=self.img_trough)

    @property
    def value(self):
        """Return bar's value'"""
        return self._value

    @value.setter
    def value(self, value):
        """Set bar's value'"""
        self._value = value
        # adjust bar height to value
        self.coords('pbar', 0, self.height, self.width, self.height*(1 - value/self.maximum))

shape = '/path/to/shape/picture'

bar1 = MyBar(root, shape)
bar2 = MyBar(root, shape)
bar3 = MyBar(root, shape)
bar4 = MyBar(root, shape)
bar5 = MyBar(root, shape=shape, value=60, bg='navy', trough_color='sky blue', bar_color='royal blue')

bar1.pack(side='left', padx=10, pady=40)
bar2.pack(side='left', padx=10, pady=40)
bar3.pack(side='left', padx=10, pady=40)
bar4.pack(side='left', padx=10, pady=40)
bar5.pack(side='left', padx=10, pady=40)

bar2.value = 75
bar3.value = 50
bar4.value = 100
root.mainloop()

結果: 屏幕截圖形狀的酒吧

此示例中用於shape的圖像: 形狀圖像

在上面的代碼中, shape是一張黑白圖片,用作 alpha 蒙版。 這個想法是,條形圖是在bar_color上繪制的Canvas矩形,背景trough_color 然后在這個欄的頂部,我放了一個顏色bg的圖像,除了透明的形狀部分已經從背景中剪下來( shape圖片的黑色部分)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM