why does passing a StringVar as an argument for a function doesn't work?


I am currently trying to build a program that shows a ktinker interface with buttons, one of which (ecrire) will write in a file the current content of a tkinter entry / input / text field. I use the "ecrire" function with a tk button. When the function uses a substring that is defined outside of it to get the entry current value, it works fine. But when I try to pass the substring as a parameter, the StringVar.get() method returns nothing. Here is the code :

# coding: utf-8

from tkinter import *

fenetre = Tk()
labl = Label(fenetre, text = "Bonjour, monde !")
bouton=Button(fenetre, text="Fermer", command=fenetre.quit)
def ecrire(ligne):
    file = open("save.txt", "a")
    message = ligne.get()
    file.write(message + "n")

string = StringVar()
entree = Entry(fenetre, textvariable=string, width=10)
btnEcrire = Button(fenetre, text="ecrire", command = ecrire())

I’m very new to python and to these libraries, and I’ll eventually need to pass StringVars as parameters to that function. What am I doing wrong ?

Asked By: SpaceMansion




btnEcrire = Button(fenetre, text="ecrire", command=lambda: ecrire(string))  

Answered By: len
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.