在CTK中获取和追加数据的问题,Python
我正在写一个程序,用来找出你导入列表中的最大值和最小值。但是当我尝试用 .get() 和 .insert() 粘贴文本时,它就是不工作。控制台上显示的错误信息说“insert 需要两个参数,但我只给了一个”。
import customtkinter as ctk
root = ctk.CTk()
root.geometry("500x500")
root.iconbitmap('/imgs/icon.ico')
root.title("Max - Min finder")
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("dark-blue")
list = []
is_there_smth = False
def clearing():
list_view.delete(0.0, "end")
list.clear()
def submitting():
global is_there_smth, get
get = list_view.get(0.0, "end")
if is_there_smth:
list_view.delete(0.0, "end")
is_there_smth = False
list.insert(float(enter.get()))
else:
is_there_smth = True
list_view.insert("end", get)
list.append(float(enter.get()))
list_view.insert("end", str(enter.get()) + ', ')
def find_max_min():
global is_there_smth, get
get = list_view.get(0.0, "end")
if is_there_smth:
list_view.delete(0.0, "end")
is_there_smth = False
else:
is_there_smth = True
list_view.insert("end", "Max:" + str(max(list)) + '\n' + "Min:" + str(min(list)))
label = ctk.CTkLabel(root, text="List", font=("Monolisa", 20))
label.pack(pady=20, padx=20)
list_view = ctk.CTkTextbox(root, width=250, height=50)
list_view.pack(pady=20, padx=20)
enter = ctk.CTkEntry(root, placeholder_text="Enter each number", font=("Monolisa", 14), width=200)
enter.pack(pady=20, padx=20)
submit = ctk.CTkButton(root, text="Submit", font=("Monolisa", 14), command=submitting)
submit.pack(pady=20, padx=20)
clear = ctk.CTkButton(root, text="Clear", font=("Monolisa", 14), command=clearing)
clear.pack(pady=20, padx=20)
findmax = ctk.CTkButton(root, text="Find max and min", font=("Monolisa", 14), command=find_max_min)
findmax.pack(pady=20, padx=20)
root.mainloop()
我还没有尝试任何方法,因为我不知道该怎么修复。
1 个回答
0
在第24行,你写了 list.insert(float(enter.get()))
如果你想在列表中插入一个值,你应该把这一行改成 list.insert(index_to_insert_to, float(enter.get()))
,然后把 index_to_insert_to 替换成你想插入的位置的索引。
如果你想在文本框中插入文本,你应该把这一行改成 list_view.insert(where_to_insert, str(float(enter.get())))
,然后把 where_to_insert 替换成你想在文本框中插入文本的位置。
总之,你忘记了插入值的位置索引。