0

import tkinter as tk
import ttkbootstrap as ttk
import random 
import pyperclip

# Basic Random Code

def randomcode_press():
    code_length = entry_letters_int.get()
    code = list(''.join(random.choice("ABCDEFGHIJKLMNOPRSTUWabcdefghijklmnoprstuwz1234567890!@#$%^&*")for i in range(code_length))) 
    random.shuffle(code)
    output_string1.set(code)
   

def randomcode(event):
    code_length = entry_letters_int.get()
    code = list(''.join(random.choice("ABCDEFGHIJKLMNOPRSTUWabcdefghijklmnoprstuwz1234567890!@#$%^&*")for i in range(code_length))) 
    random.shuffle(code)

    output_string1.set(code)
    


def copy():
    pyperclip.copy(str(output_string1))
    



# Window 
window = ttk.Window(themename='darkly')
window.geometry('450x380')
window.title("Anrunt's code generator")

# Notebook widget 

notebook = ttk.Notebook(window)

# Tab 1 - Random code Basic
tab1 = ttk.Frame(notebook)

# Title 1

title_Label1 = ttk.Label(tab1, text = "Code Generator", font = 'Calibri 20')
title_Label1.pack()

how_many_Label = tk.Label(tab1, text = 'Enter code length:', font ='Calibri 12')
how_many_Label.pack()

# Input Frame 1

input_frame2 = tk.Frame(tab1)
input_frame2.pack()


entry_letters_int = tk.IntVar()
entry_letters = ttk.Entry(master= input_frame2, textvariable=entry_letters_int)

entry_letters.bind('<Return>', randomcode)

entry_letters.pack()

# Generate Button 1
 
button1 = tk.Button(tab1, text = 'Generate', command = randomcode_press)
button1.pack(pady=10)

# Copy Button 1 

buttonC = tk.Button(tab1, text = 'Copy to clipboard', command = copy)
buttonC.pack()


# Output 1

output_string1 = tk.StringVar()
output_Label1 = ttk.Label(tab1, font = 'Calibri 15', textvariable=output_string1)

output_Label1.pack(pady=5)

# Notebook 1
notebook.add(tab1, text = 'Basic')
notebook.pack()


# Run 
window.mainloop()

Hello, I started creating simple password generator using python and tkinter ui, I have a problem with button which is meant to copy output to computer clipboard. I made button which has command = copy and created function def copy(): , unfortunately it doesnt work and it doesn't copy an output number. I already tried using return value to return code value and using it in def copy function but it still doesnt work :(

anrunt
  • 11
  • 1

1 Answers1

1

i found a solution, in function:

def copy():
    pyperclip.copy(str(output_string1))

i should have written

def copy():
    pyperclip.copy(str(output_string1.get())

Now everything works perfectly fine :)

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
anrunt
  • 11
  • 1