Tkinter récupérer une variable radio bouton dans un label

Tkinter récupérer une variable radio bouton dans un label - Python - Programmation

Marsh Posté le 18-03-2014 à 12:42:54    

Débutant en Python, mon premier programme avec Tkinter bloque sur la récupération d'une variable d'un champ radio button dans une autre fenetre type Label.
J'arrive à récupérer ma variable avec .get() et l'imprimer mais pas à l'inclure dans le Label. J'ai essayé de passer par une fonction, un lambda sans succès.
 
Voici le type de programme que j'utilise:
 
from tkinter import *
 
fen = Tk()
global var
var = IntVar()
 
def result():
    print(var.get())
     
donnee = lambda var: var.get()
 
bouton_1 = Radiobutton(fen, text="jaune", variable=var, value=1, command=result)
bouton_2 = Radiobutton(fen, text="vert", variable=var, value=2, command=result)
 
bouton_1.pack()
bouton_2.pack()
 
champ_label = Label(fen, bg='white', text=donnee, font=("Comic Sans MS","10","bold" ),relief=FLAT)
champ_label.pack()
 
fen.mainloop()
 

Reply

Marsh Posté le 18-03-2014 à 12:42:54   

Reply

Marsh Posté le 18-03-2014 à 16:34:16    

docbour a écrit :

Débutant en Python, mon premier programme avec Tkinter bloque sur la récupération d'une variable d'un champ radio button dans une autre fenetre type Label.
J'arrive à récupérer ma variable avec .get() et l'imprimer mais pas à l'inclure dans le Label. J'ai essayé de passer par une fonction, un lambda sans succès.
 
Voici le type de programme que j'utilise:
 
from tkinter import *
 
fen = Tk()
global var
var = IntVar()
 
def result():
    print(var.get())
     
donnee = lambda var: var.get()
 
bouton_1 = Radiobutton(fen, text="jaune", variable=var, value=1, command=result)
bouton_2 = Radiobutton(fen, text="vert", variable=var, value=2, command=result)
 
bouton_1.pack()
bouton_2.pack()
 
champ_label = Label(fen, bg='white', text=donnee, font=("Comic Sans MS","10","bold" ),relief=FLAT)
champ_label.pack()
 
fen.mainloop()
 


http://infohost.nmt.edu/tcc/help/p [...] ables.html

Citation :

Label
You can set its textvariable option to a StringVar. Then any call to the variable's .set() method will change the text displayed on the label.


[:petrus75]
 
(accessoirement, ton "global var" sert strictement à rien)


---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 18-03-2014 à 20:00:28    

Merci, j'ai compris le principe mais je n'arrive pas encore à le mettre en pratique

Reply

Marsh Posté le 18-03-2014 à 20:59:59    

Suffit de remplacer text=donnee par textvariable=var, j'ai du mal à percevoir ce qu'il y a à mettre en pratique.


Message édité par masklinn le 18-03-2014 à 21:00:16

---------------
I mean, true, a cancer will probably destroy its host organism. But what about the cells whose mutations allow them to think outside the box, and replicate and expand beyond their wildest dreams by throwing away the limits imposed by overbearing genetic r
Reply

Marsh Posté le 19-03-2014 à 08:57:59    

Super, c'est ce que je cherchais.
Je n'avais pas saisi la différence entre "text" et "text variable"
Merci

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed