Button Farbe ändern

Fragen zu Tkinter.
Antworten
Mätthi

Hi
Wie kann man die Farbe von einem Button ändern, wenn man ihn drückt
(normalerweise besch)?
BlackJack

@Mätthi: Die Farbe kann man nachträglich mit der `configure()`-Methode verändern: ``button.configure(background='yellow')``
EyDu
User
Beiträge: 4881
Registriert: Donnerstag 20. Juli 2006, 23:06
Wohnort: Berlin

Und die Farbe heißt "Beige" und nicht "Besch" ;-)
Das Leben ist wie ein Tennisball.
BlackJack

Jo, stimmt:

Code: Alles auswählen

In [44]: b.configure(background='besch')
---------------------------------------------------------------------------
<class `_tkinter.TclError`>               Traceback (most recent call last)

/home/bj/<ipython console> in <module>()

/home/bj/lib-tk/Tkinter.py in configure(self, cnf, **kw)
   1198         the allowed keyword arguments call the method keys.
   1199         
-> 1200         return self._configure('configure', cnf, kw)
   1201     config = configure
   1202     def cget(self, key):

/home/bj/lib-tk/Tkinter.py in _configure(self, cmd, cnf, kw)
   1189                     self.tk.call(_flatten((self._w, cmd, '-'+cnf))))
   1190             return (x[0][1:],) + x[1:]
-> 1191         self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
   1192     # These used to be defined in Widget:
   1193     def configure(self, cnf=None, **kw):

<class `_tkinter.TclError`>: unknown color name "besch"

In [45]: b.configure(background='beige')

In [46]:
:-)
yipyip
User
Beiträge: 418
Registriert: Samstag 12. Juli 2008, 01:18

Du suchst wohl eher 'activebackground' statt 'background'. Hier etwas zum Experimentieren.

Code: Alles auswählen

import Tkinter as tk

def show_pressed(but):

  but.config(activebackground='red')


def show_released(but):

  but.config(activebackground='yellow')
  
  
root = tk.Tk()
but1 = tk.Button(root, text='Button1')
show_released(but1)

but1.bind('<ButtonPress-1>', lambda ev: show_pressed(but1))
but1.bind('<ButtonRelease-1>', lambda ev: show_released(but1))
but1.pack()

but2 = tk.Button(root, text='Button2', activebackground='cyan',
                 background='besch'.replace('sch', 'ige'))
but2.pack()

root.mainloop()
Siehe auch
http://effbot.org/tkinterbook/button.htm
oder
http://infohost.nmt.edu/tcc/help/pubs/t ... utton.html

:wink:
yipyip
Zuletzt geändert von Damaskus am Samstag 20. Juli 2013, 13:27, insgesamt 1-mal geändert.
Grund: Benutzernamen entfernt
yipyip
User
Beiträge: 418
Registriert: Samstag 12. Juli 2008, 01:18

ups, das war jetzt gleichzeitg.
EyDu
User
Beiträge: 4881
Registriert: Donnerstag 20. Juli 2006, 23:06
Wohnort: Berlin

Und ich dachte immer, dass fünf Jahre Französischunterricht in der Schule umsonst waren ^^
Das Leben ist wie ein Tennisball.
yipyip
User
Beiträge: 418
Registriert: Samstag 12. Juli 2008, 01:18

Mon francais c'est tres mal...
:wink:
yipyip
Benutzeravatar
wuf
User
Beiträge: 1529
Registriert: Sonntag 8. Juni 2003, 09:50

Hallo zusammen

Hier noch die gekürzte yipyip-Variante:

Code: Alles auswählen

import Tkinter as tk

root = tk.Tk()
but1 = tk.Button(root, text='Button1', activebackground='yellow')

but1.bind('<ButtonPress-1>',
    lambda ev: but1.config(activebackground='red'))
but1.bind('<ButtonRelease-1>',
    lambda ev: but1.config(activebackground='yellow'))
but1.pack()

but2 = tk.Button(root, text='Button2', activebackground='cyan',
    background='besch'.replace('sch', 'ige'))
but2.pack()

root.mainloop()
Gruss zusammen wuf :wink:
Take it easy Mates!
Mätthi

Richtig ich hatte nur keine zeit background auszuprobieren
activebackground war eigentlich richtig
nochmal Danke
Antworten