Search code examples
pythonpython-3.xtkintertkinter-entry

How to print something from Entry in another window in Python 3?


Why it does not print the string values I have entered into the textbox in the newwindow?

from tkinter import *

def newwindow():
    newwindow = Tk()
    newwindow.title('Sign Up')
    newwindow.geometry('200x400')

    def sign_done():
        david = a.get()
        javed = b.get()
        lbee = Label(newwindow, text=david).pack()
        baeee = Label(newwindow, text=javed).pack()

    a = StringVar()
    b = StringVar()
    user = Entry(newwindow, textvariable=a).pack()
    pword = Entry(newwindow, textvariable=b).pack()

    done = Button(newwindow, text='done now', command=sign_done).pack()

    newwindow.mainloop()

root = Tk()
root.title('Gulmeena')
root.geometry("500x200")
button = Button(root, text='Go', command=newwindow).pack()

root.mainloop()

Please do not use Classes


Solution

  • Use Tk only to create main window. To create any other window use Toplevel. And use only one mainwindow().

    var = Widget(...).pack() assigns None to var because pack()/grid()/place() returns None. You have to do it in two lines.

    var = Widget(...) 
    var.pack(). 
    

    If you don't need var then you can do

    Widget(...).pack()
    

    To make code more readable

    • I use import tkinter as tk to show I use tk.Button, not ttk.Button nor my own classButton`
    • I use name user_var and password_var which means something
    • I put all functions at the beginning - even inside newwindow

    Code:

    import tkinter as tk
    
    def newwindow():
    
        def sign_done():
            david = user_var.get()
            javed = password_var.get()
            tk.Label(newwindow, text=david).pack()
            tk.Label(newwindow, text=javed).pack()
    
        newwindow = tk.Toplevel()
        newwindow.title('Sign Up')
        newwindow.geometry('200x400')
    
        user_var = tk.StringVar()
        password_var = tk.StringVar()
    
        user = tk.Entry(newwindow, textvariable=user_var)
        user.pack()
        pword = tk.Entry(newwindow, textvariable=password_var)
        pword.pack()
    
        tk.Button(newwindow, text='done now', command=sign_done).pack()
    
    root = tk.Tk()
    root.title('Gulmeena')
    root.geometry("500x200")
    
    tk.Button(root, text='Go', command=newwindow).pack()
    
    root.mainloop()
    

    You can do the same without StringVars

    import tkinter as tk
    
    def newwindow():
    
        def sign_done():
            david = user.get()
            javed = pword.get()
            tk.Label(newwindow, text=david).pack()
            tk.Label(newwindow, text=javed).pack()
    
        newwindow = tk.Toplevel()
        newwindow.title('Sign Up')
        newwindow.geometry('200x400')
    
        user = tk.Entry(newwindow)
        user.pack()
        pword = tk.Entry(newwindow)
        pword.pack()
    
        tk.Button(newwindow, text='done now', command=sign_done).pack()
    
    
    root = tk.Tk()
    root.title('Gulmeena')
    root.geometry("500x200")
    
    tk.Button(root, text='Go', command=newwindow).pack()
    
    root.mainloop()