簡體   English   中英

如何在 ttk 小部件中為一側設置邊框樣式? Tkinter

[英]How to style border for one side in ttk widgets? Tkinter

我的問題很簡單。 我想知道如何設置主題 Tkinter 小部件的邊框一側,特別是按鈕或 TButton?

檢查這個? 是修改版

from tkinter import *
from tkinter import ttk

class MyLabel(Frame):
    '''inherit from Frame to make a label with customized border'''
    def __init__(self, parent, myborderwidth=0, mybordercolor=None,
                 myborderplace='center', *args, **kwargs):
        Frame.__init__(self, parent, bg=mybordercolor)
        self.propagate(False) # prevent frame from auto-fitting to contents
        self.button = ttk.Button(self, *args, **kwargs) # make the label

        # pack label inside frame according to which side the border
        # should be on. If it's not 'left' or 'right', center the label
        # and multiply the border width by 2 to compensate
        if myborderplace == 'left':
            self.button.pack(side=RIGHT)
        elif myborderplace == 'right':
            self.button.pack(side=LEFT)
        else:
            self.button.pack()
            myborderwidth = myborderwidth * 2

        # set width and height of frame according to the req width
        # and height of the label
        self.config(width=self.button.winfo_reqwidth() + myborderwidth)
        self.config(height=self.button.winfo_reqheight())


root=Tk()
MyLabel(root, text='Hello World', myborderwidth=4, mybordercolor='red',
        myborderplace='left').pack()
root.mainloop()

暫無
暫無

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

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