Seite 1 von 1

Button Farbe ändern

Verfasst: Freitag 21. August 2009, 13:50
von Mätthi
Hi
Wie kann man die Farbe von einem Button ändern, wenn man ihn drückt
(normalerweise besch)?

Verfasst: Freitag 21. August 2009, 14:13
von BlackJack
@Mätthi: Die Farbe kann man nachträglich mit der `configure()`-Methode verändern: ``button.configure(background='yellow')``

Verfasst: Freitag 21. August 2009, 14:18
von EyDu
Und die Farbe heißt "Beige" und nicht "Besch" ;-)

Verfasst: Freitag 21. August 2009, 14:31
von 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]:
:-)

Verfasst: Freitag 21. August 2009, 14:57
von Mätthi
danke

Verfasst: Freitag 21. August 2009, 14:57
von yipyip
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

Verfasst: Freitag 21. August 2009, 14:58
von yipyip
ups, das war jetzt gleichzeitg.

Verfasst: Freitag 21. August 2009, 15:02
von EyDu
Und ich dachte immer, dass fünf Jahre Französischunterricht in der Schule umsonst waren ^^

Verfasst: Freitag 21. August 2009, 15:05
von yipyip
Mon francais c'est tres mal...
:wink:
yipyip

Verfasst: Freitag 21. August 2009, 15:58
von wuf
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:

Verfasst: Freitag 21. August 2009, 16:47
von Mätthi
Richtig ich hatte nur keine zeit background auszuprobieren
activebackground war eigentlich richtig
nochmal Danke