You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Tkinter程序不必要创建两个窗口的问题?

问题分析与修复方案

问题根源

  1. 重复创建主窗口:代码开头先执行了window=Tk()生成一个主窗口,后面又实例化继承自Tk的SeaofBTCapp类,相当于又创建了一个新的主窗口,这就是出现两个窗口的直接原因。
  2. Canvas归属错误:全局定义的canvas绑定的是开头那个多余的窗口,导致灰色栏显示在第一个窗口,而按钮、文本却在第二个窗口,造成元素分散。
  3. 按钮布局失误:StartPage里的第二个按钮用了button.pack(),应该是button2.pack(),导致这个按钮无法正常显示。

修正后的完整代码

from tkinter import *
from tkinter import Canvas

class SeaofBTCapp(Tk):
    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        container = Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne, PageTwo):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class StartPage(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        # 在当前Frame内创建Canvas,确保元素归属正确
        canvas = Canvas(self, width=1366, height=768)
        topbar = canvas.create_rectangle(0,0, 1366, 30, fill="gray")
        canvas.pack()

        label = Label(self, text="start page", font=("Verdana", 12))
        label.place(x=0, y=0)

        button = Button(self, text="Visit Page 1",
                       command=lambda: controller.show_frame(PageOne))
        button.pack()

        button2 = Button(self, text="Visit Page 2",
                        command=lambda: controller.show_frame(PageTwo))
        button2.pack()  # 修正变量名,让第二个按钮正常显示

class PageOne(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        label = Label(self, text="Page One", font=("Verdana", 12))
        label.pack(padx=10, pady=10)

        button1 = Button(self, text="back to home",
                          command=lambda: controller.show_frame(StartPage))
        button1.pack()

        button2 = Button(self, text= "page 2",
                          command=lambda: controller.show_frame(PageTwo))
        button2.pack()

class PageTwo(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        label = Label(self, text="page two", font=("Verdana", 12))
        label.pack(padx=10, pady=10)

        button1 = Button(self, text="back to home",
                            command=lambda: controller.show_frame(StartPage))
        button1.pack()

        button2 = Button(self, text="page one",
                          command=lambda: controller.show_frame(PageOne))
        button2.pack()

if __name__ == "__main__":
    window = SeaofBTCapp()
    window.title('Floor Plan')
    window.geometry("1366x768")
    window.configure(bg="white")
    window.mainloop()

核心修复点

  • 删除开头多余的window=Tk()和全局canvas定义,避免生成额外窗口。
  • 将Canvas的创建移到StartPage内部,让灰色栏和其他元素同属一个窗口。
  • 修正第二个按钮的布局代码,确保按钮正常显示。

内容的提问来源于stack exchange,提问作者j.jam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 08:29:53