Change global variable to value in entry field
<p>How can I change a global variable to a value inputted by a user in an entry field?</p>

<pre><code>card_no = 0

def cardget():
    global card_no
    card_no = e1.get()
    print(card_no)


def menu():
    global card_no
    root = Tk()

    e1 = Entry(root).pack()
    Label(root, text= "Enter card number").pack(anchor= NW)
    Button(root, text= "Confirm card", command=cardget).pack(anchor= NW)

menu()
</code></pre>

<p>Don't use global variables. Tkinter apps work much better with OOP.</p>

<pre><code>import tkinter as tk

class App:
    def __init__(self, parent):
        self.e1 = tk.Entry(parent)
        self.e1.pack()
        self.l = tk.Label(root, text="Enter card number")
        self.l.pack(anchor=tk.NW)
        self.b = tk.Button(root, text="Confirm card", command=self.cardget)
        self.b.pack(anchor=tk.NW)
        self.card_no = 0
    def cardget(self):
        self.card_no = int(self.e1.get()) # add validation if you want
        print(self.card_no)

root = tk.Tk()
app = App(root)
root.mainloop()
</code></pre>

python tkinter