0

I use Tkinter, and I wanted to print a more sophisticated text on a button. It consists of strings parts, variables that contain e.a 'MyString', and integer Variables. What it does when i grid the buttons, is following :

enter image description here

Here is the code for my first button :

bouttons_annuler[(mon_compteur_annulations_loc)].config(text = ("Vend", action_corr_numero_ventes_loc, "x", vente_suivie_loc,"a", entry_prix_loc, euros.get(), "\n","Annuler"))

and for my second button :

bouttons_annuler[(mon_compteur_annulations_loc)].config(text = ("Offre achat", action_corr_numero_achats_loc, "x", achat_suivie_loc, "Annuler"))

Where action_corr_numero_ventes_loc represents 'Facebook' ; vente_suivie_loc represents 20 (as an integer) ; entry_prix_loc represents also 20 (as an integer) ; euros.get() represents euros=StringVar(master=root) euros.set('€ :')

Why does this happen, and how can i solve this? i tried to apply the str() function to all of it, but it didn't work (wouldn't really have made sense if it worked)

2
  • format() is your friend Commented Apr 2, 2018 at 23:14
  • where do I use it? I tried using it around everything, but it gives me an error saying that format() takes at most 2 arguments. Commented Apr 2, 2018 at 23:18

2 Answers 2

1

You get the curly braces because you are giving tkinter a tuple. The curly braces are the internal tcl interpreter's method of rendering lists.

You should explicitly convert your data to a string to get rid of the curly braces. For example:

bouttons_annuler[...].config(text = ' '.join(("Vend", action_corr_numero_ventes_loc, ...)))

Of course, you don't have to use join. You can format the data however you want. If you give tkinter a tuple or list, though, you're at the mercy of it deciding how to represent your data.

Sign up to request clarification or add additional context in comments.

2 Comments

Did you actually mean reading or rending itself, over at "...method of rending lists" .
@CoolCloud: I meant "rendering". Thanks for pointing that out.
0

Try This:

Edit:

texts = 'Vend {} x {} a {} {} \n Annuler'.format(action_corr_numero_ventes_loc, vente_suivie_loc, entry_prix_loc, euro)

bouttons_annuler[(mon_compteur_annulations_loc)].config(text =texts)

4 Comments

It still gives me {€ :}
and gives me this error : bouttons_annuler[(mon_compteur_annulations_loc)].config(text = ('Vend %s x %d a %d %s' % action_corr_numero_ventes_loc, vente_suivie_loc, entry_prix_loc, euros.get())) TypeError: not enough arguments for format string
I changed texts into : texts = 'Vend {} x {} a {} {} \n Annuler'.format(action_corr_numero_ventes_loc, vente_suivie_loc, entry_prix_loc, euros.get()) (just added the s.get() after euro and the \n) and it worked! Thank you so much !
Glad i could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.