【问题标题】:Tkinter button throwing "unknown option -XXXXX" when setting options. Python 2.7设置选项时,Tkinter 按钮抛出“未知选项 -XXXXX”。蟒蛇 2.7
【发布时间】:2020-04-20 22:01:44
【问题描述】:

这是包含文件

import tkinter as tk 
from tkinter import ttk 

class CollapsiblePane(ttk.Frame): 
""" 
 -----USAGE----- 
collapsiblePane = CollapsiblePane(parent,  
                      expanded_text =[string], 
                      collapsed_text =[string]) 

collapsiblePane.pack() 
button = Button(collapsiblePane.frame).pack() 
"""

def __init__(self, parent, expanded_text ="Collapse <<", 
                           collapsed_text ="Expand >>"):


    ttk.Frame.__init__(self, parent) 
    # These are the class variable 
    # see a underscore in expanded_text and _collapsed_text 
    # this means these are private to class 
    self.parent = parent 
    self._expanded_text = expanded_text 
    self._collapsed_text = collapsed_text 

    # Here weight implies that it can grow it's 
    # size if extra space is available 
    # default weight is 0 
    self.columnconfigure(1, weight = 1) 

    # Tkinter variable storing integer value 
    self._variable = tk.IntVar() 

    # Checkbutton is created but will behave as Button 
    # cause in style, Button is passed 
    # main reason to do this is Button do not support 
    # variable option but checkbutton do 

    self._button.config( width=1, height=1, borderwidth = 0)
    self._button.grid(row = 0, column = 0) 

    # This wil create a seperator 
    # A separator is a line, we can also set thickness 
    self._separator = ttk.Separator(self, orient ="horizontal") 
    self._separator.grid(row = 0, column = 1, sticky ="we") 

    self.frame = ttk.Frame(self) 

    # This will call activate function of class 
    self._activate() 
def toggle(self): 
    """Switches the label frame to the opposite state."""
    self._variable = not self._variable 
def _activate(self): 
    if not self._variable: 

        # As soon as button is pressed it removes this widget 
        # but is not destroyed means can be displayed again 
        self.frame.grid_forget() 

        # This will change the text of the checkbutton 

        self.toggle()
    elif self._variable: 
        # increasing the frame area so new widgets 
        # could reside in this container 
        self.frame.grid(row = 1, column = 0, columnspan = 1) 

        self.toggle()

这是使用它的程序。

# Importing tkinter and ttk modules 
from tkinter import * 
from tkinter.ttk import *

# Importing Collapsible Pane class that we have 
# created in separate file 
from collapsiblepane import CollapsiblePane as cp 

# Making root window or parent window 
root = Tk() 
root.geometry('200x200') 

# Creating Object of Collapsible Pane Container 
# If we do not pass these strings in 
# parameter the the defalt strings will appear 
# on button that were, expand >>, collapse << 
cpane = cp(root, 'Expanded', 'Collapsed') 
cpane.grid(row = 0, column = 0) 

# Button and checkbutton, these will 
# appear in collapsible pane container 
b1 = Button(cpane.frame, text ="GFG").grid( 
            row = 1, column = 2, pady = 10) 

cb1 = Checkbutton(cpane.frame, text ="GFG").grid( 
                  row = 2, column = 3, pady = 10) 

mainloop()

这是我的错误。

TclError: unknown option "-borderwidth"

错误来自这一行

    self._button.config( width=1, height=1, borderwidth = 0)

我也尝试过

self._button = ttk.Button(self,command = self._activate, width=1, height=1, borderwidth = 0) 

同样的错误。

我已经删除了边框宽度,然后它会在宽度/高度方面引发相同的错误。我已经在其他情况下测试了宽度/高度/边框宽度并且它有效,我只是不确定为什么它在这里不起作用。

【问题讨论】:

  • ttk 按钮没有与 tk 按钮相同的选项。错误说的是实话。

标签: python button tkinter


【解决方案1】:

在使用 ttk 小部件时,您必须记住,您将为每个 tkinter 小部件单独设置的许多参数在涉及到时主要由样式管理ttk 小部件。

如果您查看ttk.Button 小部件选项 (here),您可以轻松找出可以设置和不可以设置的参数。

在您的情况下,您必须为按钮创建样式。

s = ttk.Style()
s.configure("TButton", borderwidth=0)

如果您使用此代码,则不必单独声明您的按钮正在使用该样式,但是每个其他 (ttk) 按钮也会自动使用此样式。如果您只想为一个按钮设置样式,则必须执行以下操作:

s = ttk.Style()
s.configure("b1.TButton", borderwidth=0) #you can rename b1 to anything you want

然后

b1 = Button(cpane.frame, text ="GFG", style="b1.TButton").grid( 
            row = 1, column = 2, pady = 10)  

您也可以自己阅读样式here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-29
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多