簡體   English   中英

tkinter按鈕縮小以適合框架

[英]tkinter buttons shrink to fit frame

有沒有一種方法可以確保按鈕縮小以適合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圖像。

問題是,當框架出現時,最后兩個按鈕沒有出現在框架中。 如果我調整整個窗口的大小,則底部的按鈕不會出現。 當主窗口調整大小時,有沒有一種方法可以動態調整按鈕的大小。 嘗試與谷歌與所有可用的選項,例如pack.propagate和其他,但似乎不適用於我。

不,tkinter無法自動縮小按鈕以使其適合框架。 您將必須自己管理所有這些。 例如,您可以將綁定添加到框架的<Configure>事件,只要框架更改大小,該事件就會被調用。 然后,您可以獲取框架的大小並遍歷子項,從而盡一切可能使事情變得合適。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM