从标签或msgbeox复制tkinter中的文本

2024-10-03 11:20:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在寻找将文本复制到剪贴板或从Tkinter gui复制结果的方法,但如果有命令或其他东西,请使用idk

这是我现在的代码,结果出现在一个消息框中,我能把它复制到剪贴板吗

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

还有一个简单的小问题,使用类还是这个表单更好


Tags: textimporttkintercolumnpassrootpasswordgrid
2条回答

上面的答案很好。事实上,这是做这件事的方法。我读了评论,他提到它只能接受字符串。这是完全错误的。它还可以接收函数。例如

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

希望这有帮助

Tkinter确实有这个功能,只是

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

希望我能帮忙

问候

相关问题 更多 >