gpt4 book ai didi

python - 第一次使用tkinter,未知错误: TclError

转载 作者:行者123 更新时间:2023-12-01 08:06:10 26 4
gpt4 key购买 nike

我正在尝试为闹钟应用程序创建一个下拉菜单,以便在闹钟激活时播放一些旧的角色扮演游戏声音。我不断收到此错误,但不知道如何修复它:

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

TclError:未知选项“-class”

我已经包含了我写的所有内容,因为我不确定错误来自哪里,但我相信它是在“### pick Alarm sound menu”之后 ''' 标准闹钟 '''

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

最佳答案

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

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

关于python - 第一次使用tkinter,未知错误: TclError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55523135/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com