user11167665
user11167665

Reputation:

First Time using tkinter, unknown error: TclError

I'm trying to create a drop down menu for a alarm clock application to play some old rpg game sounds when the alarm activates. I keep getting this error and I'm not sure how to fix it:

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

TclError: unknown option "-class"

I've included everything I wrote because I'm not sure where the error is drawing from but I believe its after '### pick alarm sound menu' ''' Standard Alarm Clock '''

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()

Upvotes: 1

Views: 754

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 385970

The root of your problem is this line, which has two fundamental bugs:

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

The first bug is that the configure method doesn't accept the argument window. That is what is actually causing the error. If you remove it, that error goes away.

The second problem is that options1.configure(...) returns None, so you're effectively doing None.grid(row = 5, column = 0), which will throw an error. You need to move the call to grid to a separate line. Also, the line after that calls pack, which you need to remove altogether.

The fixed code looks like this:

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

Upvotes: 1

Reedinationer
Reedinationer

Reputation: 5774

I edited your post to eliminate most the errors. Namely

  • .grid(sticky=W) should be -> .grid(sticky='W')
  • I used consistent formatting for import tkinter as tk
    • i.e. Label() -> tk.Label()
    • Entry() -> tk.Entry() Still though I get an error different from your post:
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"

So let's go over how to read this cause it seems you don't understand. You can see at the top line 66 (every error will look like this and will undoubtedly tell you what line it was on) So how do we fix the error? Well let's look at the line (also part of the error traceback):

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

Hmmm well it looks like it is mad at us for something to do with self._configure('configure', cnf, kw). How about we just comment out that line and see what happens?

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

Hmm still not the error you posted, but we're getting somewhere! Seems you are also mixing .grid() and .pack() (not supposed to do that) We'll just switch it to .grid() and use the default settings (you can change that on your own). Bam it works!

Final result:

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()

Seems you were trying to do something illegal with options1. A good idea when you're not sure what you can do is try print(dir(some_variable)). This will list all methods and attributes of the variable you can access. You can also read some documentation. For tkinter I prefer to use this documentation source

Upvotes: 0

Related Questions