Aktives Button farblich erkennbar machen

Fragen zu Tkinter.
Antworten
Nobuddy
User
Beiträge: 994
Registriert: Montag 30. Januar 2012, 16:38

Hallo wuf

Danke für Deinen Tipp, nach dieser kleinen Änderung, funktioniert es wie ich es mir vorgestellt habe! :wink:

Grüße Nobuddy
Nobuddy
User
Beiträge: 994
Registriert: Montag 30. Januar 2012, 16:38

Hallo zuammen,
benötige nochmal Eure Hilfe, für eine Löung.

Ich habe damals (ist ja schon eine Weile her ... :wink: ), wufś Code umgesetzt, welcher auch so prima funktioniert.

Die Sub-Buttons werden mit Klicken der Maustaste aktiviert und die Farbe des Backgrounds ändert sich auf grün.
Jetzt suche ich eine Lösung bei meinen Sub-Buttons, ohne mechanische Betätigung einer Hardware, Sub-Buttons zu aktivieren und die Farbe des Backgrounds zu verändern.

Hier mal die dazu gehörenden Codestücke:

Code: Alles auswählen

class MyButton(tk.Button):
    """Steuerung der Hauptbuttons"""

    selected_button_obj = None
    HIGHLIGHT_COLOR = 'green'
    HIGHLIGHT_CONF = dict(bg=HIGHLIGHT_COLOR,
        activebackground=HIGHLIGHT_COLOR)
   
    def __init__(self, parent, **options):
       
        try:
            self.command_call = options['command']
            del options['command']
        except KeyError:
            self.command_call = None
           
        tk.Button.__init__(self, parent, **options)
        self.bind('<Button-1>', self.callback)
        self.std_conf = dict(bg=self['bg'],
            activebackground=self['activebackground'])
       
    def callback(self, event=None):
        button_obj = MyButton.selected_button_obj
        if button_obj is not None:
            button_obj.config(self.std_conf)
               
        if self.command_call:
            self.config(MyButton.HIGHLIGHT_CONF)
            MyButton.selected_button_obj = self
            self.command_call(event.widget['text'])


class MySubButton(tk.Button):
    """Steuerung der Subbuttons"""

    selected_sub_button_obj = None
    HIGHLIGHT_COLOR = 'green'
    HIGHLIGHT_CONF = dict(bg=HIGHLIGHT_COLOR,
        activebackground=HIGHLIGHT_COLOR)
   
    def __init__(self, parent, **options):
       
        try:
            self.command_call = options['command']
            del options['command']
        except KeyError:
            self.command_call = None
               
        tk.Button.__init__(self, parent, **options)
        self.bind('<Button-1>', self.callback)
        self.std_conf = dict(bg=self['bg'],
            activebackground=self['activebackground'])
       
    def callback(self, event=None):
        sub_button_obj = MySubButton.selected_sub_button_obj
        self.mysubbutton = sub_button_obj
        if sub_button_obj is not None:
            sub_button_obj.config(self.std_conf)
               
        if self.command_call:
            self.config(MySubButton.HIGHLIGHT_CONF)
            MySubButton.selected_sub_button_obj = self
            self.command_call(event.widget['text'])
Sub-Buttons werden anhand der gewählten Haupt-Buttons erstellt:

Code: Alles auswählen

    def button_callback(self, button_name):
        """Rückruf der Haupt-Buttons"""
        print("Button (rechts): {0}".format(button_name))

        self.master_check = button_name
        self.sub_check = ''

        # Entferne alle bestehende Sub-Buttons aus dem linken Frame
        for button in self.button_frame_left.winfo_children():
            button.destroy()

        # Kontrolle: Hat der aktivierte rechte Button zugehörige Sub-Buttons?
        try:
            MySubButton.selected_sub_button_obj = None
            for button_name in self.my_sub_buttons[button_name]:
                MySubButton(self.button_frame_left,
                width=self.conf['button_width'],
                font=(self.conf['sub_font']), text=button_name,
                command=self.sub_button_callback).pack(padx=8, ipady=2, 
                pady=19, fill='x')
            # Ja es gibt Sub-Buttons!
        except KeyError:
            print("Für {} gibt es keine Liste mit Sub-Buttons!".format(
                button_name))
Ausgewähltes Sub-Button wird verarbeitet:

Code: Alles auswählen

    def sub_button_callback(self, sub_button_name):
        """Rückruf der Sub-Buttons"""
        print("Sub-Button (links): {0}".format(sub_button_name))
Mit

Code: Alles auswählen

self.sub_button_callback('Sub-Button-1')
kann ich das ausgewählte Sub-Button aktivieren, aber die Farbe des Backgrounds verändert sich nicht auf grün.

Wie kann man dies lösen?

Grüße Nobuddy
Antworten