Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to clear the clipboard everytime I add something to it?

I am working on a password generator. I have a button that generates the password and a button that copies it to the clipboard.
The problem is: Every time I generate another password and then add it to the clipboard it just gets added to the other password.
If I had the password 123ok and copied it to the clipboard but then generated 456ok and copied this one to the clipboard I would have 123ok456ok.

That´s my code:


import string
import random
import tkinter as tk

letters = string.ascii_letters
numbers = string.digits
symbols = string.punctuation

chars, nums, syms = True, True, True

evy = ""

if chars:
    evy += letters
if nums:
    evy += numbers
if syms:
    evy += symbols

length = 8

window = tk.Tk()
window.geometry("500x500")
window.title("Password Generator")
window.configure(background="black")


def generate():
    for x in range(1):
        global password
        password = "".join(random.sample(evy, length))
        pwd = tk.Label(text=password, foreground="green", background="black", font=16)
        pwd.pack()


genBtn = tk.Button(window, command=generate, text="Generate Password")
genBtn.pack()


def copy_clip():
    window.clipboard_append(password)
    window.clipboard_clear()


copyBtn = tk.Button(text="Copy the password", command=copy_clip)
copyBtn.pack()

window.mainloop()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this instead

import random
import tkinter as tk

letters = string.ascii_letters
numbers = string.digits
symbols = string.punctuation

chars, nums, syms = True, True, True

evy = ""

if chars:
    evy += letters
if nums:
    evy += numbers
if syms:
    evy += symbols

length = 8

window = tk.Tk()
window.geometry("500x500")
window.title("Password Generator")
window.configure(background="black")

password = ""  # Initialize the password variable

def generate():
    global password  # Use the global password variable
    password = "".join(random.sample(evy, length))
    pwd = tk.Label(text=password, foreground="green", background="black", font=16)
    pwd.pack()

genBtn = tk.Button(window, command=generate, text="Generate Password")
genBtn.pack()

def copy_clip():
    if password:
        window.clipboard_clear()  # Clear clipboard
        window.clipboard_append(password)
        window.update()  # Update clipboard

copyBtn = tk.Button(text="Copy the password", command=copy_clip)
copyBtn.pack()

window.mainloop()```

Try this instead

   

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading