Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set expiration date for python application

Tags:

python

tkinter

Hello dear programmers,

I have generated an EXE with pyinstaller. However, I would like this EXE to be valid for only 6 months so that users have to re-download a improved EXE with updates from a download-center. Is there any way to add this in the program code?

Unfortunately I can't include a standard update function because the users often don't have internet.

I am curious about your suggestions. Thanks a lot guys! I've attached a small snippet of the program below.

import tkinter as tk
from tkinter import ttk



class Win1:
    def __init__(self, master):
        
        self.master = master 
        self.topFrame = tk.Frame(self.master)
        self.topFrame.grid(row=0, column=0, sticky='news', ipady = 5)
        self.B_GapFrame = tk.Frame(self.master)
        self.master.resizable(False, False)         



        self.gapType = tk.StringVar(self.master)
        self.choiceGap =  ['RBC, TUR']
        self.gapType.set('') # set the default option
        self.ctngMenu = tk.OptionMenu(self.topFrame, self.gapType, *self.choiceGap, command=self.choose_gap_handle)
        self.ctngMenu.config(width=40)    
        self.LabelGap = tk.Label(self.topFrame, text="Select TYPE")
        self.LabelGap.grid(row = 3, column = 0,sticky = "W")
        self.ctngMenu.grid(row = 3, column =2,sticky = "W", columnspan = 3)
        
        
        self.frameVar = tk.StringVar(self.master)
        self.choiceFrame = "Test"
        self.frameVar.set('') # set the default option
        self.frameMenu = ttk.Combobox(self.topFrame, values= self.choiceFrame, state = "readonly", justify = "center", textvariable = self.frameVar, width = 12)
        self.frameMenu.grid(row = 1, column =2,sticky = "W", pady = 7, columnspan = 3)
        self.LabelFrame =  tk.Label(self.topFrame, text="Select TUR     ")
        self.LabelFrame.grid(row = 1, column = 0,sticky = "W",pady =7)
                                       


    def choose_gap_handle(self, selected_Gap):
            
        if selected_Gap == 'RBC, TUR':
            self.B_GapFrame.tkraise()
            self.B_GapFrame.grid(row=2, column=0, sticky='news') 

        
root = tk.Tk()
root.geometry("+50+50")
app = Win1(root)
root.mainloop()
like image 467
Djangopy Avatar asked Nov 22 '25 20:11

Djangopy


2 Answers

It all depends how 'cruel' you want to be.

One option might be to add a hard coded date in your code like

import datetime
RLS_DATE = datetime.date(2021, 02, 24)

and then before your main code section something like:

if datetime.date.today() - RLS_DATE > 7 * 30: # about 7 months
    # as @TheLizzard suggested delete the executable.
    # You might do this only if running under 
    # py2exe to avoid deleting your own source code
    if getattr(sys, 'frozen', False):
        os.remove(sys.argv[0]) 
elif datetime.date.today() - RLS_DATE > 6 * 30: # about 6 months
    # write your own coded, that shows just a popup and lets the user
    # continue using the app.

If you don't want to hardcode the release date, you could write a smarter script to create your executable and add some data for py2exe, that contains the date at which you compiled the executable.

What I mean with being cruel.

Either just show a popup, that the user should update,

or add a popup and block the user interface for x seconds. The delay might increase the longer the user doesn't update

or just exit the program

or even delete the program

Of course all these options are not safe. This is just for standard users.

If somebody insists to run your program, he can modify the generated executable to ignore the date.

like image 189
gelonida Avatar answered Nov 24 '25 11:11

gelonida


You can save your app first launch time to local file and then compare it with current time.

import os
import time
import tempfile
import tkinter as tk

EXPIRATION_TIME_SEC = 100


class App(object):
    def __init__(self):

        config = os.path.join(tempfile.gettempdir(), 'config.txt')
        self.start_time = time.time()
        if os.path.isfile(config):
            with open(config, 'r') as f:
                self.start_time = int(float(f.read()))
        else:
            with open(config, 'w') as f:
                f.write(f'{self.start_time}')

        self.root = tk.Tk()
        self.label = tk.Label(self.root)
        self.label.pack()
        self.update_label()
        self.root.mainloop()

    def update_label(self):
        if time.time() - self.start_time > EXPIRATION_TIME_SEC:
            self.root.quit()
        self.label.configure(text=f'Expired in: {int(self.start_time+EXPIRATION_TIME_SEC-time.time())} sec.')
        self.root.after(1000, self.update_label)


App()

Output:

enter image description here

like image 26
Alderven Avatar answered Nov 24 '25 10:11

Alderven



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!