Tkinter按钮在设置选项时抛出“未知选项-XXXXX”。 Python 2.7

问题描述 投票:0回答:1

这里是包含文件

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) 

存在相同错误。

我已经删除了borderwidth,然后在width / height上引发了相同的错误。我已经在其他实例中测试了width / height / borderwidth,它可以工作,但我不确定为什么它在这里不起作用。

[这是来自tkinter导入ttk类CollapsiblePane(ttk.Frame)中的tk的包含文件导入tkinter:“”“ -----用法----- collapsiblePane = CollapsiblePane(parent,...

]
python button tkinter
1个回答
0
投票

使用ttk

小部件时,必须记住,要为每个[[tkinter
© www.soinside.com 2019 - 2024. All rights reserved.