hello ,
I use Tkinter , at the line : print modelidvar in Interface3() function , it prints "" , nothing , without any error message , can i know why plz , here is all the code :

# -*- coding: cp1252 -*-
# -*- coding: cp1252 -*-
from Tkinter import *
import Tkinter as tk


#import os
import ttk
import MySQLdb;

#####################################################################################

def center(window):

    sw = window.winfo_screenwidth()
    sh = window.winfo_screenheight()
    rw = window.winfo_reqwidth()
    rh = window.winfo_reqheight()
    xc = (sw - rw) / 3
    yc = (sh -rh) / 3
    window.geometry("+%d+%d" % (xc, yc))
    window.deiconify()


##################################################################################



################################################################################




#############################################################################


def revenir():

           interf3.destroy()
           Interface2()           


#############################################################################

def valider():
        modelidvar = modelid_entry.get()
        print modelidvar
        interf2.destroy()
        Interface3()



#############################################################################    


def Interface3() :

    global interf3
    global modelidvar

    interf3 = Tk()    
    interf3.geometry("500x400")
    interf3.title('                            Base de données ')  

    f1 = Frame(interf3, bg="blue",  width=500, height=400)
    f1.pack( fill=X, expand=0)   


    lab5= Label(interf3, text="NumBon" , bg = "blue", fg = "white" )
    lab5.place ( x=26 , y=100)

    lab6= Label(interf3, text="DateBon" , bg = "blue", fg = "white" )
    lab6.place ( x=85 , y=100)

    lab7= Label(interf3, text="Total/Bon" , bg = "blue", fg = "white" )
    lab7.place ( x=400 , y=100)


    print modelidvar




    brevenir = Button(interf3, text = " revenir ", command = revenir)
    brevenir.place( x=12, y=266)

    Quitter = Button(interf3, text = "Quitter" , command = interf3.destroy)
    Quitter.place( x=230, y=350 )

    interf3.after(0,center,interf3)


    interf3.mainloop()


################################################################################

def Interface2() :

    global interf2

    fenetre.destroy()

    interf2 = Tk()    
    interf2.geometry("500x400")
    interf2.title('                            Base de données ')  

    f1 = Frame(interf2, bg="blue",  width=500, height=400)
    f1.pack( fill=X, expand=0)

    lab2 = Label(interf2, text="Veuillez saisir le numero d'Import du Model ( num+année , ex: 122013 )\n et cliquer sur valider " , bg = "blue", fg = "white" )
    lab2.place ( x=80 , y=45 )

    global modelidvar

    modelidvar = StringVar()

    lab3= Label(interf2, text="Référence Model :" , bg = "blue", fg = "white" )
    lab3.place ( x=30 , y=100)

    modelid_entry = ttk.Entry(interf2, width=13, textvariable=modelidvar)
    modelid_entry.place (x = 140 , y = 100 )

    modelidvar = modelid_entry.get()




    bvalider = Button(interf2, text = " Valider ", command = valider)
    bvalider.place( x=250, y=100)        

    #revenir = Button(interf2, text = " revenir ", command = revenir)
    #revenir.place( x=12, y=266)

    Quitter = Button(interf2, text = "Quitter" , command = interf2.destroy)
    Quitter.place( x=230, y=350 )

    interf2.after(0,center,interf2)



    interf2.mainloop()







################################################################################    


 ############################################################################   





#####################################


fenetre=Tk()
fenetre.geometry("500x400")
fenetre.title('                                     ')

f1 = Frame(fenetre, bg="blue",  width=500, height=400)
f1.pack( fill=X, expand=0)

lab1 = Label(fenetre, text="Gestion de ressources informatiques" , bg = "blue", fg = "white" )
lab1.place ( x=152 , y=25 )

failure_max = 3

global uservar
global passvar


uservar = StringVar()
passvar = StringVar()

Label(fenetre, text = "User name:" , bg = "blue", fg = "white" ).place(x = 66, y = 100)
Label(fenetre, text = "Password:" , bg = "blue", fg = "white" ).place(x = 66, y = 125)

entryu = ttk.Entry(fenetre , textvariable=uservar)
entryu.place (x = 180 , y = 100)

entryp = ttk.Entry(fenetre, show='*', textvariable=passvar)
entryp.place (x = 180 , y = 125)




b = Button(fenetre, borderwidth=2, text="Login", width=7, pady=0, command=Interface2)#, image=photo, compound=CENTER)#, state=DISABLED)
b.place(x = 220 , y = 160)

frame21 = Frame(fenetre , bg="blue",    width=40, height=40)
frame21.pack( fill=X, side = BOTTOM)

buttonquit = Button(fenetre, text = "Quitter", command = fenetre.destroy)        
buttonquit.place( x=224 , y=350 )




##################################



##################################

#Display

fenetre.after(0,center,fenetre)
fenetre.mainloop()

Dani AI

Generated

Quick diagnosis: the value printed in Interface3 is the empty string because the module-level name holding the entry value was overwritten (and never updated) before the second window opened. As hinted, there are two separate scoping problems happening together — the StringVar is replaced too early when the second window is built, and the callback that reads the Entry creates a local name instead of updating the module-level name that Interface3 reads.

Practical fixes (in order of increasing quality)

  • Minimal: stop overwriting the StringVar at creation time. Keep the variable as the StringVar bound to the Entry and read it later with its get() method when needed. That way the value is taken at button-press time, not when the UI was constructed.
  • If a plain string is preferred: make the callback update the global explicitly (declare the name global inside the callback) or call the StringVar.set(...) from the callback so the module-level variable reflects the typed text before Interface3 prints it.
  • Better: avoid globals entirely. Pass the typed text as an argument when opening the next window — e.g. call Interface3(entry.get()) from the button callback so Interface3 receives the value directly.

Debugging tips

  • Print both the value and its type (repr(value), type(value)) right before calling Interface3 to confirm whether the name is a StringVar or a plain string. That immediately shows whether the name was overwritten early.
  • Be careful which names are local to a function (Entry widget variables defined inside the window constructor) versus module globals; accessing a local name from a different function will either fail or give surprising results unless it was explicitly put into a shared scope.

Mention to : the quickest way to see the problem is to remove the early assignment that replaces the StringVar and then print modelidvar.get() (or pass the text into Interface3) so the value printed reflects the current Entry contents.

Using global variables is clumsy and leads to errors. You should declare modlidvar as global in function valider() to be consistent.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.