0

I been searching for methods to copy text to clipboard or copy the results from Tkinter gui but idk if there is a command or something

here is my code for now here the result comes in a messagebox can i copy it to clipboard

import tkinter.messagebox
import string
import random


def qs_msgbbox():   # qs_msgbbox
    tkinter.messagebox.showinfo("Info", "For customer support or tip or rating contact:"
                                        "dghaily725@gmail.com\npress the button for generated pass\nmsg will appear then copy\nthe generated password")


def gen_pass(k=9):  # gen_pass
    char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*"
    password = ''
    for i in range(9):
        password += random.choice(char)
    tkinter.messagebox.showinfo("Password", password)
root = Tk()
root.title("Password Generator")

lbl1 = Label(root, text="Generate Password", bd=2, relief=SUNKEN, height=5, width=50, bg='black', fg='white')
lbl1.configure(font=(70))
lbl1.grid(row=0, column=2)

lbl2 = Label(root, text='For more information press the Question mark', bd=2, relief=SUNKEN, fg='red')
lbl2.configure(font=(70))
lbl2.grid(row=0, column=0, pady=10)


btn1 = Button(root, text='Press to Generate', height=5, width=50, bg='grey', command=gen_pass)
btn1.configure(font=(70))
btn1.grid(row=1, column=2, padx=460, pady=50)

btn2photo = PhotoImage(file='question.png')
btn2 = Button(root, image=btn2photo, width=30, height=30, command= qs_msgbbox)
btn2.grid(row=0, column=1)

root.mainloop()

and also just a quick small question is it better to use classes or this form

Paties
  • 47
  • 11
  • 1
    Does this answer your question? [How do I copy a string to the clipboard on Windows using Python?](https://stackoverflow.com/questions/579687/how-do-i-copy-a-string-to-the-clipboard-on-windows-using-python) – jizhihaoSAMA Aug 24 '20 at 13:02

2 Answers2

3

Tkinter does have a function for that, simply just

from tkinter import Tk
root = Tk()

root.clipboard_clear()
root.clipboard_append("Something to the clipboard")
root.update() # the text will stay there after the window is closed

Hope I could help

Greets

Ari24
  • 370
  • 5
  • 12
  • can i do it like root.clipboard_append(name_of_function or variable) – Paties Aug 24 '20 at 13:04
  • This function only accepts a string (afaik) so if you have function that returns a string, use it: root.clipboard_append(get_some_text_here()) this will automatically call the function and get the return value. Variables also works fine – Ari24 Aug 24 '20 at 13:08
2

The above answer is perfectly fine. Infact its the method to do it. I read the comments, He had mentioned that it could only take in string. That is completely false. It can also take in functions. For example..

import tkinter as tk
root = tk.Tk()

#creating a entry Widget.(Labels are fine as well)
entry = tk.Entry(root)
entry.pack()

#NOW if you want to copy the whole string inside the above entry box after you 
typed  in #

def copy ():#assign this function to any button or any actions
    root.clipboard_clear()
    root.clipboard_append(entry.get())   #get anything from the entry widget.

root.mainloop()

Hoping this was helpful

Duffy
  • 123
  • 7