Reputation: 17
I am trying to make a simple calculator using python (tkinter GUI). However, when I try and increment my pointer variable, it does not work. From what I could figure out, this was caused because my pointer variable was not global and I did not declare it as a global variable. But even after I did that, it is still not working. Can someone explain why?
from tkinter import *
root = Tk()
root.title("Calculator (Simple)")
entrybox = Entry(root, width = 40, borderwidth= 5)
entrybox.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
global pointer
pointer = 0
def button_entry(num):
pointer += 1
number = entrybox.get() + num
entrybox.delete(0, END)
entrybox.insert(0, number)
print(pointer)
Upvotes: 0
Views: 59
Reputation: 38
global pointer
should be inside button_entry
inside the function on 1st line.
Upvotes: 1
Reputation: 26
You have to put global pointer in the function, not outside, because it says to python interpreter that you need to use a variable that is not described in this function and it's in global scope
from tkinter import *
root = Tk()
root.title("Calculator (Simple)")
entrybox = Entry(root, width = 40, borderwidth= 5)
entrybox.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
pointer = 0
def button_entry(num):
global pointer
pointer += 1
number = entrybox.get() + num
entrybox.delete(0, END)
entrybox.insert(0, number)
print(pointer)
Upvotes: 1