[Python] Creating a bordered Button in Tkinter

Discussão em 'Python' iniciado por Stack, Junho 5, 2025.

  1. Stack

    Stack Membro Participativo

    I want to create a class for a Tk.Button in a Tk.Frame so that the button can have a border color independent of its background color.

    I wrote this code which creates a button in a frame just as I want but I cannot later get or set the text, bcolor and bg of a previously created FrameButton.

    I thought I should be able to do this @profile and @bgsetter constructions but these give me an error.

    ''' import tkinter as tk

    class FramedButton(tk.Frame):
    def __init__(self, parent, text="HELLO", bcolor="red", *args, **kwargs):
    tk.Frame.__init__(self, parent, bg="green", *args, **kwargs)
    self.parent = parent
    self.button=tk.Button(self, text=text, bg=bcolor, borderwidth=1)
    self.button.grid(row=0, column=0, padx=5, pady=5)
    self.pack()

    root = tk.Tk()
    root.title="FramedButton"

    fb=FramedButton(root)
    fb.pack(padx=10,pady=10)

    # but these do not work... what do I need to add/change?
    fb.text="WORLD"
    fb.bg="blue"
    fb.bcolor="yellow"

    root.mainloop()


    '''

    Continue reading...

Compartilhe esta Página