首次使用tkinter,未知错误:TclError

时间:2019-04-04 18:55:40

标签: python tkinter

我正在尝试为闹钟应用程序创建一个下拉菜单,以便在闹钟激活时播放一些旧的rpg游戏声音。我不断收到此错误,但不确定如何解决:

self.tk.call(_flatten((self._w,cmd))+ self._options(cnf))

TclError:未知选项“ -class”

我已经包含了我写的所有内容,因为我不确定错误是从哪里产生的,但是我相信它是在“ ###选择警报声音菜单”之后发出的。     '''     标准闹钟     '''

import sys
import tkinter as tk 
import time
#import pygame

#pygame.init()

### load sounds
'''
opening_music = pygame.mixer.Sound("01 - Opening.ogg")
prelude_music = pygame.mixer.Sound("02 - Prelude.ogg")
nations_rage_music = pygame.mixer.Sound("03 - Nations Rage.ogg")
sanctuary_music = pygame.mixer.Sound("04 - Sanctuary.ogg")
reunion_music = pygame.mixer.Sound("05 - Reunion.ogg")
rebels_be_music = pygame.mixer.Sound("06 - Rebels Be.ogg")
'''

### create music list
music_lst = ['opening_music', 'prelude_music', 'nations_rage_music', 
             'sanctuary_music', 'reunion_music', 'rebels_be_music']

### window configuration:
window = tk.Tk()
window.title("Alarm Clock")
window.configure(background='gray')

### clock function:
def ticktock():
    clock_time_string = time.strftime('%H:%M:%S')
    clock.config(text = clock_time_string)
    clock.after(200,ticktock)

### alarm set label:
tk.Label(window, text = "Alarm Set", fg = "black", bg = 'grey', font = "none 12 bold").grid(row = 2, column = 0, sticky = 'W')

### alarm string entry box:
alarm_string = tk.Entry(window, width = 20, bg = 'white')
alarm_string.grid(row = 3, column = 0, sticky = 'W')

### pick alarm sound menu
def change(*args):
    var.get()

tk.Label(text = "Alarm Sounds", fg = 'black', bg = 'gray', font = 'none 12 bold').grid(row = 4, column = 0, sticky = 'W')
music_var = tk.StringVar(window)
music_var.set(music_lst[0])
music_var.trace('w', change)

options1 = tk.OptionMenu(window, music_var, music_lst[0], music_lst[1], music_lst[2], music_lst[3], music_lst[4], music_lst[5])
options1.configure(window, font = "none 12 bold").grid(row = 5, column = 0, sticky = 'W')
options1.pack()


### alarm function
def alarm(alarm_string_hour):
    while alarm_string:
        if alarm_string == clock_time_string('%H:%M:%S'):
            pass
            ## play sound
            ## try / except
            ## clear alarm

clock = tk.Label(window, font = ('times', 100, 'bold'), bg = 'grey')
clock.grid(row = 1, column = 0, sticky = 'W')
ticktock()
clock.mainloop()

2 个答案:

答案 0 :(得分:1)

问题的根源是此行,它具有两个基本错误:

options1.configure(window, font = "none 12 bold").grid(row = 5, column = 0, sticky = 'W')

第一个错误是configure方法不接受参数window。这实际上是导致错误的原因。如果将其删除,该错误就会消失。

第二个问题是options1.configure(...)返回None,因此您实际上在执行None.grid(row = 5, column = 0),这将引发错误。您需要将对grid的呼叫移到另一行。另外,此后的行将调用pack,您需要将其完全删除。

固定代码如下:

options1.configure(font = "none 12 bold")
options1.grid(row = 5, column = 0, sticky = 'W')

答案 1 :(得分:0)

我编辑了您的帖子,以消除大多数错误。即

  • .grid(sticky=W)应该是-> .grid(sticky='W')
  • 我对import tkinter as tk使用了一致的格式
    • Label()-> tk.Label()
    • Entry()-> tk.Entry() 尽管我收到与您的帖子不同的错误:
Traceback (most recent call last):
  File "C:/Users/rparkhurst/PycharmProjects/Workspace/workspace.py", line 66, in <module>
    options1.configure(window, font = "none 12 bold").grid(row = 5, column = 0, sticky = 'W')
  File "C:\Program Files\Python 3.5\lib\tkinter\__init__.py", line 1330, in configure
    return self._configure('configure', cnf, kw)
  File "C:\Program Files\Python 3.5\lib\tkinter\__init__.py", line 1321, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: unknown option "-use"

因此,让我们看一下如何阅读此内容,因为您似乎不理解。您可以在顶部line 66看到(每个错误看起来都是这样,并且无疑会告诉您错误所在的行)。那么,我们如何解决该错误?好吧,让我们看一下这一行(也是错误回溯的一部分):

options1.configure(window, font = "none 12 bold").grid(row = 5, column = 0, sticky = 'W')

嗯,好像对我们self._configure('configure', cnf, kw)有点生气。我们只是注释掉那行,看看会发生什么?

Traceback (most recent call last):
  File "C:/Users/rparkhurst/PycharmProjects/Workspace/workspace.py", line 68, in <module>
    options1.pack()
  File "C:\Program Files\Python 3.5\lib\tkinter\__init__.py", line 1990, in pack_configure
    + self._options(cnf, kw))
_tkinter.TclError: cannot use geometry manager pack inside . which already has slaves managed by grid

嗯,仍然不是您所发布的错误,但是我们已经到了!似乎您也在混用.grid().pack()(不应该这样做),我们只是将其切换到.grid()并使用默认设置(您可以自行更改)。糟糕!

最终结果:

import sys
import tkinter as tk
import time
#import pygame

#pygame.init()

### load sounds
'''
opening_music = pygame.mixer.Sound("01 - Opening.ogg")
prelude_music = pygame.mixer.Sound("02 - Prelude.ogg")
nations_rage_music = pygame.mixer.Sound("03 - Nations Rage.ogg")
sanctuary_music = pygame.mixer.Sound("04 - Sanctuary.ogg")
reunion_music = pygame.mixer.Sound("05 - Reunion.ogg")
rebels_be_music = pygame.mixer.Sound("06 - Rebels Be.ogg")
'''

### create music list
music_lst = ['opening_music', 'prelude_music', 'nations_rage_music',
             'sanctuary_music', 'reunion_music', 'rebels_be_music']

### window configuration:
window = tk.Tk()
window.title("Alarm Clock")
window.configure(background='gray')

### clock function:
def ticktock():
    clock_time_string = time.strftime('%H:%M:%S')
    clock.config(text = clock_time_string)
    clock.after(200,ticktock)

### alarm set label:
tk.Label(window, text = "Alarm Set", fg = "black", bg = 'grey', font = "none 12 bold").grid(row = 2, column = 0, sticky = 'W')

### alarm string entry box:
alarm_string = tk.Entry(window, width = 20, bg = 'white')
alarm_string.grid(row = 3, column = 0, sticky = 'W')

### pick alarm sound menu
def change(*args):
    var.get()

tk.Label(text = "Alarm Sounds", fg = 'black', bg = 'gray', font = 'none 12 bold').grid(row = 4, column = 0, sticky = 'W')
music_var = tk.StringVar(window)
music_var.set(music_lst[0])
music_var.trace('w', change)

options1 = tk.OptionMenu(window, music_var, music_lst[0], music_lst[1], music_lst[2], music_lst[3], music_lst[4], music_lst[5])
#options1.configure(window, font = "none 12 bold").grid(row = 5, column = 0, sticky = 'W')
options1.grid()


### alarm function
def alarm(alarm_string_hour):
    while alarm_string:
        if alarm_string == clock_time_string('%H:%M:%S'):
            pass
            ## play sound
            ## try / except
            ## clear alarm

clock = tk.Label(window, font = ('times', 100, 'bold'), bg = 'grey')
clock.grid(row = 1, column = 0, sticky = 'W')
ticktock()
clock.mainloop()

似乎您正在尝试对options1进行非法操作。当您不确定自己可以做什么时,一个好主意是尝试print(dir(some_variable))。这将列出您可以访问的变量的所有方法和属性。您还可以阅读一些文档。对于tkinter,我更喜欢使用this documentation source