使用 Tkinter 从 Messagebox 复制

编程入门 行业动态 更新时间:2024-10-18 10:20:50
本文介绍了使用 Tkinter 从 Messagebox 复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我用 Tkinter 编写了一个密码生成器,并设置了一个 messagebox,当网站的数据已经在数据库中时它会弹出.是否有允许我从弹出窗口复制文本的选项?因为这些密码真的很长.或者我需要进入保存它的文件来复制它吗?

I've written a password generator with Tkinter and have set a messagebox that pops-up when the data for the website is already in the database. Is there an option that allows me to copy the text from the pop-up? Because these passwords are really long. Or do I need to go into the file where it is saved to copy it?

messagebox.showinfo(title=website, message=f" Email: {email}\nPassword: {password}")

推荐答案

试试这个:

import tkinter as tk


class Popup:
    def __init__(self, title:str="Popup", message:str="", master=None):
        if master is None:
            # If the caller didn't give us a master, use the default one instead
            master = tk._get_default_root()

        # Create a toplevel widget
        self.root = tk.Toplevel(master)
        # A min size so the window doesn't start to look too bad
        self.root.minsize(200, 40)
        # Stop the user from resizing the window
        self.root.resizable(False, False)
        # If the user presses the `X` in the titlebar of the window call
        # self.destroy()
        self.root.protocol("WM_DELETE_WINDOW", self.destroy)
        # Set the title of the popup window
        self.root.title(title)

        # Calculate the needed width/height
        width = max(map(len, message.split("\n")))
        height = message.count("\n") + 1
        # Create the text widget
        self.text = tk.Text(self.root, bg="#f0f0ed", height=height,
                            width=width, highlightthickness=0, bd=0,
                            selectbackground="orange")
        # Add the text to the widget
        self.text.insert("end", message)
        # Make sure the user can't edit the message
        self.text.config(state="disabled")
        self.text.pack()

        # Create the "Ok" button
        self.button = tk.Button(self.root, text="Ok", command=self.destroy)
        self.button.pack()

        # Please note that you can add an icon/image here. I don't want to
        # download an image right now.
        ...

        # Make sure the user isn't able to spawn new popups while this is
        # still alive
        self.root.grab_set()
        # Stop code execution in the function that called us
        self.root.mainloop()

    def destroy(self) -> None:
        # Stop the `.mainloop()` that's inside this class
        self.root.quit()
        # Destroy the window
        self.root.destroy()


def show_popup():
    print("Starting popup")
    Popup(title="title", message="Message on 1 line", master=root)
    print("Ended popup")
    print("Starting popup")
    Popup(title="title", message="Message\nOn 2 lines", master=root)
    print("Ended popup")

root = tk.Tk()
root.geometry("300x300")

button = tk.Button(root, text="Click me", command=show_popup)
button.pack()

root.mainloop()

它只是一个简单的类,其行为很像 messagebox.showinfo.如果需要,您可以添加图标.请注意,缺少某些功能,但它应该适用于您的代码.

It's just a simple class that behaves a lot like messagebox.showinfo. You can add an icon if you want. Please note that some of the functionality is missing but it should work with your code.

有关我使用的函数的更多信息,请阅读文档.这里是非官方的.

For more info on the functions that I used please read the docs. Here are the unofficial ones.

这篇关于使用 Tkinter 从 Messagebox 复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 06:13:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390656.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Tkinter   Messagebox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!