Bonsoir,
Un fil sur les webradios m'a donné envie d'utiliser tkinter pour l'améliorer .
Du coup, je souhaiterais que lorsqu'un Button est sélectionné, il exécute non seulement une commande, mais de plus que sa couleur de fond change (tant qu'un autre Button n'est pas sélectionné).
J'ai vu pas mal de choses, avec des classes (mais pas fortiche là-dessus).
J'ai pensé à une variable globale mais sans succès.
J'en suis rendu là :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import vlc
import tkinter as tk
 
wr = [ "http://direct.franceinfo.fr/live/franceinfo-midfi.mp3",
       "http://direct.franceinter.fr/live/franceinter-midfi.mp3",
       "http://direct.fipradio.fr/live/fip-webradio4.mp3",
       "http://cdn.nrjaudio.fm/audio1/fr/30201/mp3_128.mp3",
       "http://direct.francebleu.fr/live/fbarmorique-midfi.mp3",
       "https://ais-live.cloud-services.paris:8443/rfm.mp3",
       "http://cdn.nrjaudio.fm/audio1/fr/30601/mp3_128.mp3",
       "http://icecast.rtl2.fr/rtl2-1-44-128?listen=webCwsBCggNCQgLDQUGBAcGBg.mp3",
       "http://stream.ouifm.fr/ouifm-high.mp3",
       "http://cdn.nrjaudio.fm/adwz1/fr/30407/mp3_128.mp3?origine=fluxradios" ]
 
vlc = vlc.Instance()
radio = vlc.media_player_new()
 
CouleurBouton="white"
 
def lecture(chaine):
    num_chaine=int(chaine)
    url = wr[num_chaine]
    w = vlc.media_new(url)
    radio.set_media(w)
    radio.play()
 
#CouleurBouton="grey85"
def change_color(couleur):
    tk.Button.configure(bg=couleur)
 
def quitter():
    radio.release()
    root.destroy()
 
root = tk.Tk()
root.wm_title("Webradios")
 
frame1 = tk.Frame(root,bg="#fb0")
frame1.grid(row=0, column=1, sticky=tk.W+tk.E+tk.N+tk.S)
 
frame2 = tk.Frame(root,bg='#49A')
frame2.grid(row=1, column=1, sticky=tk.W+tk.E+tk.N+tk.S)
 
tk.Button(frame1, text='France Info', command=lambda: [lecture(0),change_color("#51CAF9")]).pack(fill=tk.BOTH,padx=5,pady=5)
tk.Button(frame1, text='France Inter', command=lambda: lecture(1)).pack(fill=tk.BOTH,padx=5,pady=5)
tk.Button(frame1, text='FIP Monde', command=lambda: lecture(2)).pack(fill=tk.BOTH,padx=5,pady=5)
tk.Button(frame1, text='Chérie FM', command=lambda: lecture(3)).pack(fill=tk.BOTH,padx=5,pady=5)
tk.Button(frame1, text='France Bleu Armorique', command=lambda: lecture(4)).pack(fill=tk.BOTH,padx=5,pady=5)
tk.Button(frame1, text='RFM', command=lambda: lecture(5)).pack(fill=tk.BOTH,padx=5,pady=5)
tk.Button(frame1, text='Nostalgie', command=lambda: lecture(6)).pack(fill=tk.BOTH,padx=5,pady=5)
tk.Button(frame1, text='RTL2', command=lambda: lecture(7)).pack(fill=tk.BOTH,padx=5,pady=5)
tk.Button(frame1, text='Oui FM', command=lambda: lecture(8)).pack(fill=tk.BOTH,padx=5,pady=5)
tk.Button(frame1, text='Rires & Chansons', command=lambda: lecture(9)).pack(fill=tk.BOTH,padx=5,pady=5)
 
tk.Button(frame2, text='Quitter',fg="red",command=quitter).pack(padx=5,pady=5)
 
tk.mainloop()
Avec l'erreur suivante lorsque je clique sur le premier bouton :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
    return self.func(*args)
  File "/media/marco5/Disque-4To/Informatique/python_marco/python-radio/radio-vlc-2.py", line 52, in <lambda>
    tk.Button(frame1, text='France Info', command=lambda: [lecture(0),change_color("#51CAF9")]).pack(fill=tk.BOTH,padx=5,pady=5)
  File "/media/marco5/Disque-4To/Informatique/python_marco/python-radio/radio-vlc-2.py", line 37, in change_color
    tk.Button.configure(bg=couleur)
TypeError: configure() missing 1 required positional argument: 'self'
Si quelqu'un peut me donner un coup de main, c'est sans doute très simple.