I get an error like this
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\81902\anaconda3\lib\tkinter\_init__.py", line 1892, in__call__
return self.func(*args)
File "C:\Users\81902\AppData\Local\Temp/ipykernel_5336/2499828448.py", line 35, in sub_window
read_base=config ["Fixed Phase" ]
File "C:\Users\81902\anaconda3\lib\configparser.py", line 963, in__getitem__
raise KeyError(key)
KeyError: 'Fixed Phase'
Here's the code:
#Import
import tkinter as tk
from tkinter import ttk
import configparser
# Instances for configuration files
config=configparser.ConfigParser()
# Specify the ini file to load
config.read("settings.ini")
### Function ###
def sub_window():
# Create Subwindow
fp_window=tk.Toplevel(root)
# Label Frame 1
frame1 = ttk.Labelframe(fp_window, text="Registration",
padding=10)
frame1.pack (padx=20, pady=10)
# label
reg_label=tk.Label(frame1, text="Fixed Phase:")
reg_label.pack(side=tk.LEFT, anchor=tk.W)W)
# fixed sentence input field
reg_box=tk.Entry (frame1, width=50)
reg_box.pack (side=tk.LEFT)
# save button
save_button=tk.Button(frame1, text="Save")
save_button.pack (padx=10, side=tk.LEFT)
# Label Frame 2
frame2 = ttk.Labelframe(fp_window, text="Save Slot",
paddin=10)
frame2.pack (padx=20, pady=5, fill=tk.X)
# radio button
radio_value=tk.IntVar()
read_base=config ["Fixed Phase" ]
rdio_1=ttk.Radiobutton(frame2,text=
read_base.get("phrase1"),
variable=radio_value, value=1)
rdio_1.grid(row=0, column=0, sticky=tk.W)W)
rdio_2=ttk.Radiobutton(frame2,text=
read_base.get("phrase2"),
variable=radio_value, value=2)
rdio_2.grid(row=1, column=0, sticky=tk.W)W)
rdio_3=ttk.Radiobutton(frame2,text=
read_base.get("phrase3"),
variable=radio_value, value=3)
rdio_3.grid(row=2, column=0, sticky=tk.W)W)
rdio_4 = ttk.Radiobutton(frame2,text=
read_base.get("phrase4"),
variable=radio_value, value=4)
rdio_4.grid(row=3, column=0, sticky=tk.W)W)
rdio_5=ttk.Radiobutton(frame2,text=
read_base.get("phrase5"),
variable=radio_value, value=5)
rdio_5.grid(row=4, column=0, sticky=tk.W)W)
# Set button
set_button=tk.Button(fp_window, text="Set")
set_button.pack (padx=20, pady=10, ipady=5, fill=tk.X)
### GUI###
# Create Window
root=tk.Tk()
# frame
frame=ttk.Frame(root, padding=5)
frame.pack (padx=5, pady=5)
# text box
txtbox=tk.Text(frame, width=60, height=20)
# scrollbar creation
yscroll=tk.Scrollbar(frame, orient=tk.VERTICAL,
command=txtbox.yview)
yscroll.pack (side=tk.RIGHT, fill=tk.Y)
txtbox ["yscrollcommand"] = yscroll.set
# Text Box Placement
txtbox.pack()
# Creating a Menu Bar
menubar=tk.Menu(root)
root.configure (menu=menubar)
# File Menu
filemenu=tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label="File", menu=filemenu)
# File Menu Contents
filemenu.add_command(label="Open...")
filemenu.add_command(label="Save as...")
filemenu.add_separator()
filemenu.add_command(label="Exit",
command=lambda:root.destroy())
# Help menu
helpmenu=tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label="Help", menu=helpmenu)
# > Manual
helpmenu.add_command(label="Manual")
# stationary text button
fp_button=tk.Button(root, text="Fixed Phase",
command=sub_window)
fp_button.pack (padx=10, pady=10, ipady=5, fill=tk.X)
# maintenance of window conditions
root.mainloop()
The configparser KeyError:
occurs in both cases where the path of the ini file does not exist and the key does not exist.
Try executing the code below.
importos
setting_path=os.getcwd()+os.sep+"settings.ini"
if notos.path.exists(setting_path):
print(setting_path, "There's no such thing as that.")
If you see a message like "C:\Users\XXXX\AppData\Local\Temp\ipykernel_5336\settings.ini at runtime, it seems that settings.ini could not be placed in the current directory.
If you are using the Jupiter Notebook and you must have set the current directory as well, Change the initial directory of the Jupiter Notebook or Check to see if there is a workaround when the initial directory on the Jupiter Notebook could not be changed.
If nothing is output during the previous code execution, the file has been loaded.
Check the Fixed Phrase
in the ini file for typos.
© 2024 OneMinuteCode. All rights reserved.