'How increment student roll numbers continuously

I am python beginner. I am building small school system using python and sqlite3. I want to record student registration. I created a student file in python. One of the widgets which should be filled is student roll numbers. I want it to be automatically incremented. I also want the next new roll number to be shown in the widget entry.

I have used below codes which i have got from this website. It helped me to solve half the problem. But every time I close and open the form, it starts the roll number from 0 rather than continuing next roll numbers. Really i would appreciate your help. Thanks.

from tkinter import *

def clicked():
    rollNo.set(rollNo.get()+1)

window = Tk()
window.title("Programme")
window.geometry('350x250')

rollNo = IntVar()

label = Label(window, textvariable=rollNo).grid(column=0,row=0)

button = Button(window, text="Push Me", command=clicked).grid(column=1, 
row=2)

window.mainloop()


Solution 1:[1]

from tkinter import *

window = Tk()
window.title("Programme")
window.geometry('350x250')
rollNo = IntVar()

try : 
    a = open('rolls.txt','r')
    num_roll = a.readlines()
    rollNo.set(int(num_roll[-1]))
    a = open('rolls.txt','w')

except:
    a = open('rolls.txt','w')

def clicked():
    a.write(str(rollNo.get()+1)+'\n')
    rollNo.set(rollNo.get()+1)





label = Label(window, textvariable=rollNo).grid(column=0,row=0)

button = Button(window, text="Push Me", command=clicked).grid(column=1, 
row=2)


window.mainloop()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ayyoub ESSADEQ