[Python] Why can't I change the color and text of a Tkinter GUI?

Discussão em 'Python' iniciado por Stack, Junho 6, 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

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


    The fb.text, fb.bg, and fb.bcolor lines do not work. What do I need to change so that they work?

    Continue reading...

Compartilhe esta Página