在类中如何让第二个画布出现在 tkinter 顶级窗口上?

发布于 2025-01-10 02:08:57 字数 1934 浏览 4 评论 0原文

我正在使用 Tkinter 创建一个应用程序,通过这个 Dashboard 类,我试图获取一个弹出窗口来显示另一个画布,因此我可以在弹出窗口上使用 create_image 和 tag_bind 。我当前得到的结果是第二个画布出现在第一个画布上,而不是出现在弹出窗口中。

import tkinter as tk
from tkinter import *

class Dashboard(tk.Tk):
    """
    Configures, and displays the Dashboard
    """

    def __init__(self):
        tk.Tk.__init__(self)
        self.config(width=1440, height=1024)

        canvas = tk.Canvas(self, bg="#343333", height=1024, width=1440, bd=0, highlightthickness=0, relief="ridge")
        canvas.place(x=0, y=0)

        # Captures the background image for the canvas
        image_path = "dashboard_background.png"
        self.background_img = tk.PhotoImage(file=image_path)
        canvas.create_image(0, 0, anchor='nw', image=self.background_img)

        def logoutbuttonClicker():
            pop = Toplevel(self)
            pop.geometry('537x273')
            pop.config(height=273, width=537)

            logout_canvas = tk.Canvas(canvas, bg="#ffffff", height=273, width=537, bd=0, highlightthickness=0, relief="ridge")
            logout_canvas.place(x=0, y=0)

            self.logout_background_img = PhotoImage(file=f"logout_background.png")
            logout_canvas.create_image(268.5, 136.5, anchor='nw', image=self.logout_background_img)
            
            self.logout_yes_img = PhotoImage(file=f"logout_yes.png")
            self.logout_no_img = PhotoImage(file=f"logout_no.png")

        logout_image_path = "dashboard_logout.png"
        self.logout_image = tk.PhotoImage(file=logout_image_path)
        logoutButton = canvas.create_image(45, 950, anchor='nw', image=self.logout_image)
        canvas.tag_bind(logoutButton, "<ButtonRelease-1>", lambda event: logoutbuttonClicker())

def main():
    app = Dashboard()
    app.mainloop()

if __name__ == '__main__':
    main()

这是它当前在 UI 中的显示方式

I'm using Tkinter to create an application, and with this Dashboard class, I'm trying to get a pop out window to show another canvas, so I can use create_image and tag_bind on the pop out. The result I'm currently getting is that the second canvas appears over the first canvas instead of in the pop out window.

import tkinter as tk
from tkinter import *

class Dashboard(tk.Tk):
    """
    Configures, and displays the Dashboard
    """

    def __init__(self):
        tk.Tk.__init__(self)
        self.config(width=1440, height=1024)

        canvas = tk.Canvas(self, bg="#343333", height=1024, width=1440, bd=0, highlightthickness=0, relief="ridge")
        canvas.place(x=0, y=0)

        # Captures the background image for the canvas
        image_path = "dashboard_background.png"
        self.background_img = tk.PhotoImage(file=image_path)
        canvas.create_image(0, 0, anchor='nw', image=self.background_img)

        def logoutbuttonClicker():
            pop = Toplevel(self)
            pop.geometry('537x273')
            pop.config(height=273, width=537)

            logout_canvas = tk.Canvas(canvas, bg="#ffffff", height=273, width=537, bd=0, highlightthickness=0, relief="ridge")
            logout_canvas.place(x=0, y=0)

            self.logout_background_img = PhotoImage(file=f"logout_background.png")
            logout_canvas.create_image(268.5, 136.5, anchor='nw', image=self.logout_background_img)
            
            self.logout_yes_img = PhotoImage(file=f"logout_yes.png")
            self.logout_no_img = PhotoImage(file=f"logout_no.png")

        logout_image_path = "dashboard_logout.png"
        self.logout_image = tk.PhotoImage(file=logout_image_path)
        logoutButton = canvas.create_image(45, 950, anchor='nw', image=self.logout_image)
        canvas.tag_bind(logoutButton, "<ButtonRelease-1>", lambda event: logoutbuttonClicker())

def main():
    app = Dashboard()
    app.mainloop()

if __name__ == '__main__':
    main()

This is how it currently appears in the UI

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文