Skcoder
Skcoder

Reputation: 315

How to save events in tkinter?

I made an app in Tkinter and I am running into a few problems. So when I click a button a function should perform every 1 millisecond. However, once I exit the program and open it again, the computer forgets that I already clicked the button before and does not run the function automatically. I have to click the button again for the function to work. Is there a way for the computer to remember that I already clicked the button?

Here is my code so far:

from tkinter import *

root = Tk()

def declareFunction():
    print("Hello World")

def runFunction():
    declareFunction()
    root.after(1, runFunction)

button = Button(root, text="Example", command=runFunction)

root.mainloop()

Is there a way for the computer to remember that I clicked the button, and run the function forever without me clicking on it again?

Upvotes: 0

Views: 354

Answers (1)

P S Solanki
P S Solanki

Reputation: 1123

Well, the only way round this is to save that information (that the button has been clicked) to a file or database because the computer forgets everything about the program once you exit out of it. To make the data persistent there needs to be some external file or DB. Now there are so many options on storing data.

A few of them are pickle , Shelve , Plain text files, Databases like sqlite3 or better.

I will demonstrate what you want to do with plain text file.(must read docs to know about functionalities that these modules provide)

Using Plain Text files

The easiest of them.

Create a file, store False in there (to indicate button has not been clicked). When the button is clicked the first time, change the file contents from False to True and call the function. When the program loads up, read the file, see the contents. If it says True, just run the function (and disable the button to prevent clicking as it's already been clicked. ). If it says False, just write your usual program.

NOTE you might want to disable the button to prevent clicking again as the function will just keep on running. Else up to your needs.

Example

import tkinter as tk


def declareFunction():
    print('LoL')
    
def runFunction():
    with open('test.txt', 'w') as f:
        f.write('True')

    # you might wanna disable the button here as well.
    declareFunction()
    root.after(1, runFunction)


if __name__ == '__main__':    
    with open('test.txt', 'r+') as f:
        d = f.read()
        if d == '':   # write False to file as it is empty
            f.seek(0)
            f.write('False')
            f.truncate()
            root = tk.Tk()
            tk.Button(root, text='Run Function', command=runFunction).pack()
            root.mainloop()

        elif d == 'False':  # if file says False - button was not clicked so create one
            root = tk.Tk()
            tk.Button(root, text='Run Function', command=runFunction).pack()
            root.mainloop()

        elif d == 'True':   # if file says True - button has been clicked
            root = tk.Tk()
            runFunction()    # run the function already as button was clicked
            # disable the button to prevent clicking again
            tk.Button(root, text='run function', state=tk.DISABLED, command=runFunction).pack()
            root.mainloop()

Upvotes: 1

Related Questions