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

Tkinter报错:未知选项"-height",按钮尺寸修改及布局问题求助

Hey there! Let's work through your two Tkinter issues step by step—they're both tied to how ttk components handle styling and layout, so it's easy to fix once you know the quirks.

1. Fixing the Button Height/Font Error

The problem here is that you're using ttk.Button (since you imported from tkinter.ttk import *, which overwrites the native tkinter Button), and ttk buttons don't support direct height or width parameters like the native tkinter buttons do. Those parameters only work for the original tk.Button class.

To adjust the button's size and font with ttk, you need to use a custom Style configuration instead. Here's how to fix it:

# Replace your existing button-related code with this
s = Style()
# Create a custom button style: set font and padding (padding controls button size)
s.configure('Custom.TButton',
            font=('Arial', 12),  # Adjust font family and size here
            padding=(15, 8))     # (horizontal padding, vertical padding) to tweak width/height

# Apply the custom style to your button (no more height/width parameters!)
startbutton = Button(frame4, text="Start Clustering", style='Custom.TButton')
startbutton.pack(side=RIGHT, padx=5, pady=5)

If you really want to use the native tkinter button (which lets you use height/width directly), just explicitly reference tk.Button (adjust your import to import tkinter as tk for clarity, then write):

startbutton = tk.Button(frame4, text="Start Clustering", height=3, width=15, font=('Arial',12))

But sticking with ttk components will give you more consistent styling across different operating systems.

2. Shrinking the Red Frame's Width

First, you forgot to apply your red background style to frame4—right now it's still using the default frame style. Let's fix that first, then adjust the layout to narrow its width:

  1. Apply the red style to frame4:
s = Style()
s.configure('My.ConsoleFrame', background='red')
frame4 = Frame(self, style='My.ConsoleFrame')  # Add the style parameter here
  1. To shrink the frame's width, ditch the fill=BOTH, expand=True in pack()—that forces the frame to fill the entire parent container. Instead, use parameters that let the frame only take as much space as it needs:
# This makes the frame stick to the bottom-right, only wide enough for the button
frame4.pack(side=BOTTOM, anchor=E, padx=5, pady=5)

Or if you want the frame to span the bottom but not stretch fully, use fill=X with expand=False:

frame4.pack(side=BOTTOM, fill=X, expand=False, padx=5, pady=5)

This will make it take the full horizontal space but only the vertical space needed for the button.

Full Modified Code

Here's the complete code with both fixes applied:

import tkinter as tk
from tkinter.ttk import *

class Example(Frame):
    def __init__(self,master):
        super().__init__()
        master.minsize(width=350, height=160)
        master.maxsize(width=650, height=500)
        self.initUI()

    def initUI(self):
        self.master.title("Hank (version 3)")
        self.pack(fill=BOTH, expand=True)

        # Dataset frame
        frame1 = Frame(self)
        frame1.pack(fill=X)
        lbl1 = Label(frame1, text="Dataset file_name", width=18)
        lbl1.pack(side=LEFT, padx=5, pady=5)
        entryDataset= Entry(frame1)
        entryDataset.pack(fill=X, padx=5, expand=True)

        # Row/Col/Begin frame
        frame2 = Frame(self)
        frame2.pack(fill=X)
        lblRow = Label(frame2, text="Row", width=6)
        lblRow.pack(side=LEFT, padx=5, pady=5)
        entryRow = Entry(frame2, width=5)
        entryRow.pack(side=LEFT, padx=0, expand=True)
        lblCol = Label(frame2, text="Column", width=7.5)
        lblCol.pack(side=LEFT, padx=5, pady=5)
        entryCol = Entry(frame2, width=5)
        entryCol.pack(side=LEFT, padx=5, expand=True)
        lblBegin = Label(frame2, text="Start at", width=6)
        lblBegin.pack(side=LEFT, padx=5, pady=5)
        entryBegin = Entry(frame2, width=5)
        entryBegin.pack(side=LEFT, padx=0, expand=True)

        # Console window frame
        s = Style()
        s.configure('My.TFrame', background='grey')
        frame3 = Frame(self, style='My.TFrame')
        frame3.pack(fill=BOTH, expand=True)

        # Button frame (red background)
        s = Style()
        s.configure('My.ConsoleFrame', background='red')
        frame4 = Frame(self, style='My.ConsoleFrame')
        # Adjust pack parameters to shrink width
        frame4.pack(side=BOTTOM, anchor=E, padx=5, pady=5)

        # Custom button style
        s.configure('Custom.TButton',
                    font=('Arial', 12),
                    padding=(15, 8))
        startbutton = Button(frame4, text="Start Clustering", style='Custom.TButton')
        startbutton.pack(side=RIGHT, padx=5, pady=5)

def main():
    root = tk.Tk()
    root.geometry("300x160+300+160")
    app = Example(root)
    root.mainloop()

if __name__ == '__main__':
    main()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:25:25