【问题标题】:TclError: unknown option "-font" for button in tkinterTclError:tkinter 中按钮的未知选项“-font”
【发布时间】:2020-11-13 15:38:45
【问题描述】:

我正在 python 中编写一个函数来在 tkinter 中显示窗口的组件。显示按钮时出现此错误:

TclError: unknown option "-font"

这是我的代码:

    from tkinter import *
    from tkinter.ttk import *
    import socket
    import psutil 
    from plyer import notification 
    master = Tk()
    navbar = Canvas(master,width=width,highlightthickness=0, height=60, bg='#0a2145')
    name = Label(master, text="Estancia", font=('Verdana',12,'italic'))
    name.config(background='#0a2145', foreground='white')   
    sys_det = Label(master, text='Estancia', font=('Verdana',32))
    sys_det.config(background='#304566',foreground='white')
    ID = Entry(master, font=('Verdana',10,'italic'), width=20)
    Pass = Entry(master, font=('Verdana',10),show='•', width=20)
    det_sub = Button(master, text = 'Submit',font=('Verdana',10), command=redirect, fg='white', bg='#304566', 
                    height=1, width=2)
    
    canvas1.create_window(50, 22, window=name)
    canvas1.create_window(680,20,window=navbar)
    canvas1.create_window(685,225,window=sys_det)
    canvas1.create_window(685,325,window=ID)
    canvas1.create_window(685,350,window=Pass)
    canvas1.create_window(685,380,window=det_sub)

谁能找出问题所在?

【问题讨论】:

  • 可能你使用的是 Ttk Button,而不是普通的 Tkinter 版本——你必须使用 Ttk 的 Style 机制来自定义它的小部件。 (如果您已按要求发布了minimal reproducible example,我们将能够看到您的导入语句,并准确说明您正在使用哪些小部件。)
  • 我将添加导入语句。
  • 删除字体属性时,控制台显示按钮的fg属性有错误
  • 您使用的是ttk.Button 而不是tkinter.Button,因为from tkinter.ttk import * 覆盖了from tkinter import *。避免使用import *,而是使用import tkinter as tk 和from tkinter import ttk。然后指定你想要的Button,比如tk.Button。
  • 成功了。非常感谢。一旦您将其添加为答案,我就可以接受它。

标签: python python-3.x button tkinter


【解决方案1】:

问题在于如何导入模块:

from tkinter import *
from tkinter.ttk import *

由于tkinter 和tkinter.ttk 都有Button 小部件,因此您使用tkinter.ttk.Button 而不是tkinter.Button。 tkinter.ttk.Button 不支持font 选项。

避免使用import *,使用:

import tkinter as tk
from tkinter import ttk

然后指定你想要的Button,比如tk.Button。

【讨论】:

    【解决方案2】:

    cmets 已经描述了您的问题,但 IMO 他们没有深入解释您的选择或您的问题到底是什么。您收到的错误是您真正问题的征兆,该问题是namespace pollution。当您from tkinter import * 时,您会公开 tk 包的所有类。 ttk 有许多与tk 重叠的类。因此,当您随后 from tkinter.ttk import * 时,您将覆盖 tk 包中的所有冗余类。

    立即解决方法是停止污染您的命名空间。您可以这样做:import tkinter as tk, tkinter.ttk as ttk。现在,当您想使用 tk 小部件时,您只需在类名前面加上 tk 别名。 ttk 也一样,但使用了 ttk 别名。

    TK 示例:
    label = tk.Label(self, text="some text")

    TTK 示例:
    label = ttk.Label(self, text="some text")


    解决了这个问题,您可以专注于下一部分。 tk 小部件希望您将选项放在类构造函数或 .configure() 方法中。 ttk 小部件希望您创建一个样式并将该样式应用于小部件。创建从“非常简单”到“非常复杂”的样式有多种语法可能性。如果您打算使用ttk 小部件,您应该研究ttk.Style,并可能编写一些示例代码,以便您习惯它的复杂性。不要让你没有一个例子......

    ttk 样式的复杂示例。这是一个视觉示例,让您了解预期的内容。我没有复制/粘贴此脚本的所有依赖项,因此,您无法复制/粘贴它并期望它运行。

    # Scrollbar
    # states ~ active disabled
    # NOTE: arrowsize will change the thickness of the scrollbar even if you rip the arrows out
    @dataclass
    class Scrollbar_dc:
        background:         String = Theme.Contrast
        bordercolor:        String = Theme.Base
        darkcolor:          String = Theme.Contrast
        lightcolor:         String = Theme.Contrast
        troughcolor:        String = Theme.Topcoat
        arrowcolor:         String = Theme.Trim
        gripcount:          Int = 3
        arrowsize:          Int = 14
    
    class CustomTheme(Style):
        def __init__(self, basetheme='clam'):
            Style.__init__(self)
            
            self.theme_create('custom', basetheme, {
                'custom.Vertical.TScrollbar': {
                    #this is a direct copy of the default layout
                    #it is intended to be a reference against the arrowless version, for comparison purposes
                    'layout': [('Vertical.Scrollbar.trough', 
                        {'sticky': 'ns', 'children': [
                            ('Vertical.Scrollbar.uparrow', {'side': 'top', 'sticky': ''}), 
                            ('Vertical.Scrollbar.downarrow', {'side': 'bottom', 'sticky': ''}), 
                            ('Vertical.Scrollbar.thumb', {'expand': '1', 'sticky': 'nswe'})
                        ]}
                    )],
                    'configure': asdict(Scrollbar_dc())
                },
                'custom.Horizontal.TScrollbar': {
                    'configure': asdict(Scrollbar_dc())
                },
                'arrowless.Vertical.TScrollbar': {
                    'layout': [('Vertical.Scrollbar.trough',
                        {'sticky': 'ns', 'children': [
                            ('Vertical.Scrollbar.thumb', {'expand': '1', 'sticky': 'nswe'})
                        ]}
                    )],
                    'configure': asdict(Scrollbar_dc()),                     # arrowsize will still size the scrollbar thickness
                },
                'arrowless.Horizontal.TScrollbar': {
                    'layout': [('Horizontal.Scrollbar.trough',
                        {'sticky': 'we', 'children': [
                            ('Horizontal.Scrollbar.thumb', {'expand': '1', 'sticky': 'nswe'})
                        ]}
                    )],
                    'configure': asdict(Scrollbar_dc())                      # arrowsize will still size the scrollbar thickness
                }
            })
            self.theme_use('custom')
            
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      • 1970-01-01
      • 2020-01-03
      • 2021-07-25
      • 2015-02-14
      • 2016-12-12
      相关资源
      最近更新 更多