You need to enable JavaScript to run this app. 如何实现TKinter GUI全控件随窗口动态缩放(含多弹出窗适配)
优惠活动
大模型
产品
解决方案
定价
更多

如何实现TKinter GUI全控件随窗口动态缩放(含多弹出窗适配)

Tkinter GUI 全界面动态适配 开发需求

核心功能要求

  • 开发基于Tkinter的GUI项目,实现全界面动态适配效果:调整窗口尺寸时,按钮、标签、输入框、背景图等所有界面元素均可自动缩放,适配新的窗口尺寸。
  • 现有实现进度:已完成背景图的动态缩放功能,主界面的标签、按钮等控件仍为固定尺寸,未实现动态适配。
  • 新增开发需求:在主界面新增8个分别触发独立新窗口的按钮,所有弹出的Toplevel新窗口也需要实现同等的尺寸动态适配效果。

当前已编写的基础代码

from tkinter import *
from PIL import Image, ImageTk

class MainScreen(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.configure(background="black")
        self.image = Image.open("bg.jpg")

        # 背景图像承载标签
        self.background = Label(self)
        self.background.place(x=0, y=0)

        self.bind('<Configure>', self._resize_image)

        # 欢迎文本标签
        Label(root, text="Welcome", bg="#12355F", fg="white",
              font="tahoma 12 bold").place(x=10, y=10)

        # 功能入口标签
        Label(root, text="Add:", bg="#164883", fg="white",
              font="tahoma 10 bold").place(x=10, y=80)

        # 打开新窗口的内部方法
        def openNewWindow():
            def close_window():
                newWindow.destroy()

            newWindow = Toplevel(master)
            newWindow.title("New Window")
            window_height = 565
            window_width = 970
            screen_width = newWindow.winfo_screenwidth()
            screen_height = newWindow.winfo_screenheight()
            x_cordinate = int((screen_width / 2) - (window_width / 2))
            y_cordinate = int((screen_height / 2) - (window_height / 2))
            newWindow.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))
            newWindow.configure(background="#071530")

            # 文本输出框
            output = Text(newWindow, width=75, height=6, wrap=WORD, background="white")
            output.grid(row=2, column=0, columnspan=10, sticky=W, padx=170, pady=30)

            # 窗口标题标签
            Label(newWindow, text="BLABLA", bg="#071530", fg="white", font="calibre 20 bold").grid(row=0,
            column=0,
            sticky=W,
            padx=340,
            pady=30)
            Label(newWindow, text="Subtext.",
            bg="black", fg="white", font="calibre 12 bold").grid(row=1, column=0, sticky=W, padx=230, pady=10)

            # 操作提示标签
            Label(newWindow, text="CLICK", bg="black", fg="white", font="calibre 12 bold").grid(row=3,
            column=0,
            sticky=W,
            padx=320,
            pady=10)
            Label(newWindow, text="EXIT", bg="black", fg="white", font="calibre 12 bold").grid(row=3,
            column=0,
            sticky=W,
            padx=550,
            pady=10)

            # 窗口操作按钮
            Button(newWindow, text="Exit", width=6, command=close_window, bg="orange").grid(row=4, column=0, sticky=W,
            padx=570, pady=1)
            Button(newWindow, text="View", width=6, bg="orange").grid(row=4, column=0, sticky=W, padx=350, pady=1)
            newWindow.mainloop()


        # 主界面第一个功能按钮:触发Add功能新窗口
        self.button = Button(self, text="Add", width=4, bg="orange", command=openNewWindow)
        self.button.place(x=220, y=79.4)


    def _resize_image(self,event):
        if event.widget is self:
            # 调整背景图尺寸适配框架大小
            image = self.image.resize((event.width, event.height))
            self.background_image = ImageTk.PhotoImage(image)
            self.background.configure(image=self.background_image)


root = Tk()
root.title("GUI")

window_height = 565
window_width = 970

screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()

x_cordinate = int((screen_width/2) - (window_width/2))
y_cordinate = int((screen_height/2) - (window_height/2))

root.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))

e = MainScreen(root)
e.pack(fill=BOTH, expand=1)

root.mainloop()

内容的提问来源于stack exchange,提问作者DNC

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.28 16:24:03