lundi 21 août 2023

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()




Aucun commentaire:

Enregistrer un commentaire