首页 > 解决方案 > 在 Python 中使用 tkinter 打包帧的问题

问题描述

我有 2 个课程用于 2 个不同的窗口。在主窗口中,单击一个按钮会打开第二个窗口。

我试图在第二个窗口上打包的框架显示在第一个主窗口上。

LABEL2 和 Init 按钮(类活动)出现在主窗口中,而所有其他按钮出现在新活动窗口中。我怎样才能解决这个问题?

此外,就最佳 GUI 编程实践而言,这还好吗?

以下是代码

class MainWindow(tk.Frame):

    def __init__(self, master):

        HEIGHT = 500
        WIDTH = 600
        super(MainWindow, self).__init__()
        self.canvas = tk.Canvas(self.master, height = HEIGHT, width = WIDTH)
        self.canvas.pack()
        self.FRAME = tk.Frame(self.master, bd = 5).place(relwidth=1, relheight =0.5, relx = 0, rely=0)
        self.LABEL = tk.Label(self.FRAME, text="Keravalon BizDev").place(relwidth = 1, relheight = 0.05, relx=0,rely=0)

        # Code to add widgets will go here...


        self.B = tk.Button(self.FRAME, text ="Campaigns", command = lambda: self.campaign_win())
        self.C = tk.Button(self.FRAME, text ="Analysis", command = self.helloCallBack)

        self.B.place(relx = 0.5, rely = 0.1, anchor = 'n')
        self.C.place(relx = 0.5, rely = 0.15, anchor = 'n')

    def helloCallBack(self):
        messagebox.showinfo( "Hello Python", "Hello World")

    def campaign_win(self):
        root_camp = tk.Tk()
        app2 = campaigns(root_camp)
        root_camp.mainloop()

class campaigns(tk.Frame):

    def __init__(self, master):

        super(campaigns, self).__init__()
        self.FRAME2 = tk.Frame(self.master, width=100, height =50).place(x=700,y=0)
        self.LABEL2 = tk.Label(self.FRAME2, text="Campaigns").pack()

        # Code to add widgets will go here...


        self.Init = tk.Button(self.FRAME2, text ="Initialize", command = self.helloCallBack)
        self.Skr = tk.Button(master, text ="Update Skrapp", command = self.helloCallBack)
        self.Hub = tk.Button(master, text ="Update HubSpot", command = self.helloCallBack)
        self.Wood = tk.Button(master, text ="Update Woodpecker", command = self.helloCallBack)
        self.Ex_NB = tk.Button(master, text ="Export for NeverBounce", command = self.helloCallBack)
        self.NB = tk.Button(master, text ="Update NeverBounce", command = self.helloCallBack)
        self.Ex_WP = tk.Button(master, text ="Export for Woodpecekr", command = self.helloCallBack)

        self.Init.pack()
        self.Skr.pack()
        self.Hub.pack()
        self.Wood.pack()
        self.Ex_NB.pack()
        self.NB.pack()
        self.Ex_WP.pack()    

    def helloCallBack(self):
        messagebox.showinfo( "Hello Python", "Hello World")

标签: pythonuser-interfacetkinter

解决方案


推荐阅读