Professional project
Python code solution and explanation
Tkinter GUI
Introduction
A common issue for writers is writer’s block, which is the inability to come up with ideas or put them into words. One solution to this is using The Most Dangerous Writing App, an online text editor that deletes all progress if the user stops writing for 5 seconds, as indicated by a countdown timer.
You might have come across an online disappearing text writing app, but today we’re going to make one using python, so you can use it for productivity!
Step breakdown
Let’s break this program down into smaller steps.
- Make the screen
- Display all widgets (label, textbox)
- Check if user has been inactive for 5 seconds
- Delete all text from the page
Solution
We’re going to import tkinter and add two extra variables we will use later on.
import tkinter as tk
timer = 0
has_written = False
Let’s set up the GUI screen for our program. This will be the window.
window = tk.Tk()
window.title("Disappearing Text Writing App")
window.geometry('600x800')
window.configure(bg="black")
This will be a short description at the top of the page.
intro = "Don't type for 5 seconds, everything gets deleted!"
words_label = tk.Label(window, text=intro, font=('Arial', 14), bg="black", fg="gray")
words_label.pack(padx=10, pady=10)
This will be the textbox in which the user will write. This textbox will have a special function called bind()
, where on the press of any key, the check_flag()
function will be triggered.
textbox = tk.Text(window, height=40, font=('Arial', 12), bg="black", fg='white')
textbox.bind("<Key>", check_flag)
textbox.pack(padx=20, pady=10)
Finally, we can add window.mainloop()
so the code keeps running.
window.mainloop()
Now that we’re done with the GUI window of our program, we can start with the functionality. We already entered a command check_flag()
.
Need to explain this
def check_flag(_event=None):
global has_written
if has_written:
reset_timer()
else:
has_written = True
countdown()
def countdown():
global has_written
if has_written:
global timer
timer += 1
if timer >= 5:
delete_text()
has_written = False
window.after(1000, countdown)
def reset_timer():
global timer
timer = 0
def delete_text():
textbox.delete("1.0", "end")
reset_timer()
final code
To view my full code, please visit my GitHub repository: https://github.com/Gursehaj-Singh/disappearing-text-writing-app
import tkinter as tk
timer = 0
has_written = False
# ---------- FUNCTIONS ---------- #
def check_flag(_event=None):
global has_written
if has_written:
reset_timer()
else:
has_written = True
countdown()
def countdown():
global has_written
if has_written:
global timer
timer += 1
if timer >= 5:
delete_text()
has_written = False
window.after(1000, countdown)
def reset_timer():
global timer
timer = 0
def delete_text():
textbox.delete("1.0", "end")
reset_timer()
# ---------- GUI ---------- #
window = tk.Tk()
window.title("Disappearing Text Writing App")
window.geometry('600x800')
window.configure(bg="black")
intro = "Don't type for 5 seconds, everything gets deleted!"
words_label = tk.Label(window, text=intro, font=('Arial', 14), bg="black", fg="gray")
words_label.pack(padx=10, pady=10)
textbox = tk.Text(window, height=40, font=('Arial', 12), bg="black", fg='white')
textbox.bind("<Key>", check_flag)
textbox.pack(padx=20, pady=10)
window.mainloop()