【问题标题】:tkinter buttons shrink to fit frametkinter 按钮缩小以适合框架
【发布时间】:2015-07-07 16:34:14
【问题描述】:

有没有办法确保按钮缩小以适合 tkinter 框架。

这是我的代码。

import tkinter as tk
import dashboard as da
from PIL import Image, ImageTk

class Dashboard(): 
    def __init__(self, root):
        global colors
        self.root=root
        root.title("NATT SERVER DASHBOARD VIEW")

        headerPanel = tk.Frame(root, background=colors["grey2"], width=800, height=100)
        accountPanel = tk.Frame(headerPanel, background=colors["grey3"], width=100, height=100)
        infoPanel = tk.Frame(headerPanel, background=colors["grey1"], bd=2, width=800, height=50)
        buttonPanel = tk.Frame(root, background=colors["grey2"], width=100, height=700)
        canvasPanel = tk.Frame(root, background=colors["white"], width=700, height=700)

        # pack these panels
        accountPanel.pack(side="left",fill="both")
        infoPanel.pack(side="bottom", fill="x")
        headerPanel.pack(fill="x")
        canvasPanel.pack(side="right", fill="both", expand=True)
        buttonPanel.pack(side="left", fill="y")

        self._user_account(accountPanel)
        self._create_header(headerPanel)
        self._create_info_panel(infoPanel)
        self._create_buttons(buttonPanel)
        self._create_canvas(canvasPanel)

    def _create_buttons(self, parent):  #======================{{{
        global colors
        img = Image.open("icons/server-status-icon-100x100.png")
        photo = ImageTk.PhotoImage(img)
        b1=tk.Button(parent, image=photo, bg=colors["grey1"], command=self._server_status)
        b1.image = photo
        b1.pack(side="top", anchor = "n")

        img = Image.open("icons/application-icon-100x100.png")
        photo = ImageTk.PhotoImage(img)
        b2=tk.Button(parent, image=photo, bg=colors["grey1"], command=lambda: self._contents("black", "version.log"))
        b2.image = photo
        b2.pack(side="top", anchor = "n")

        img = Image.open("icons/clean-up-icon-100x100.png")
        photo = ImageTk.PhotoImage(img)
        b3=tk.Button(parent, image=photo, bg=colors["grey1"], command=lambda: self._contents("black", "cleanup_03_07_2015_18_05_15.jag"))
        b3.image = photo
        b3.pack(side="top", anchor = "n")

        img = Image.open("icons/log-display-icon-100x100.png")
        photo = ImageTk.PhotoImage(img)
        b4=tk.Button(parent, image=photo, bg=colors["grey1"], command=self._log_display)
        b4.image = photo
        b4.pack(side="top", anchor = "n")

        img = Image.open("icons/link-status-icon-100x100.png")
        photo = ImageTk.PhotoImage(img)
        b5=tk.Button(parent, image=photo, bg=colors["grey1"], command=self._link_status)
        b5.image = photo
        b5.pack(side="top", anchor = "n")

        img = Image.open("icons/defect-resolution-icon-100x100.png")
        photo = ImageTk.PhotoImage(img)
        b6=tk.Button(parent, image=photo, bg=colors["grey1"], command=lambda: self._contents("black", "version.log"))
        b6.image = photo
        b6.pack(side="top", anchor = "n")

        img = Image.open("icons/app-process-icon-100x100.png")
        photo = ImageTk.PhotoImage(img)
        b7=tk.Button(parent, image=photo, bg=colors["grey1"], command=self._process_output)
        b7.image = photo
        b7.pack(side="top", anchor = "n")


if __name__== '__main__':
    root=tk.Tk()
    board=Dashboard(root)
    root.mainloop()

您看到的图像是 100X100 png 图像。

问题是,当框架出现时,最后两个按钮没有出现在框架中。如果我调整整个窗口的大小,底部的按钮不会出现。有没有办法在主窗口调整大小时动态调整按钮大小。 尝试使用 google 提供所有可用选项,例如 pack.propagate 等,但它似乎对我不起作用。

【问题讨论】:

    标签: python button tkinter autoresize


    【解决方案1】:

    不,tkinter 不能自动缩小按钮以适应框架。您将不得不自己管理所有这些。例如,您可以将绑定添加到框架的<Configure> 事件,只要框架改变大小,就会调用该事件。然后,您可以获取框架的大小并迭代孩子,做任何您需要做的事情来适应。

    【讨论】:

    • @jagpreet:不,抱歉。我从未见过有人尝试过。这并不容易,因为 tkinter 无法缩放图像,除非是两倍。做更多的事情需要其他库。这应该是可能的,但我怀疑这是否值得付出努力。更简单的解决方案是创建两种或三种尺寸的图标,以便您的应用可以换入和换出。
    • 好吧,我需要再次对整个框架进行返工。虽然我有其他想法,但如果可以的话,我正在尝试一条捷径。我很高兴再次收到您的专家意见。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 2020-12-21
    相关资源
    最近更新 更多