I don't know why the search button doesn't work:
import tkinter
from tkinter import messagebox
import openpyxl as xl
import customtkinter
app = customtkinter.CTk()
app.geometry("1280x720")
app.title("Panelmatica")
customtkinter.set_appearance_mode("dark")
FontBig = font=("Arial",25)
FontMed = font=("Arial",20)
def search():
file=xl.load_workbook('Data.xlsx')
sheet=file\["Sheet1"\]
Search_res = Search_Entry.get()
if Search_res == xl.cell.read_only(sheet,column = \['A'\]):
YesText=customtkinter.CTkLabel(master=app,text="yes",font=FontMed)
YesText.place(x=222,y=222)
def Add():
file=xl.load_workbook('Data.xlsx')
sheet=file\["Sheet1"\]
ID_Code =Add_ID_Entry.get()
Location=Add_Location_Entry.get()
sheet.cell(column=1,row=sheet.max_row +1,value =ID_Code)
sheet.cell(column=2,row=sheet.max_row,value =Location)
file.save('Data.xlsx')
messagebox.showinfo(title="Success",message="success")
Create_Button = customtkinter.CTkButton(master=app,text ="Add",width=200,height=100,font=FontBig,fg_color="#2e2e2e",command=Add)
Create_Button.place(x=10,y=555)
Search_Button = customtkinter.CTkButton(master=app,text ="Search",width=200,height=100,font=FontBig,fg_color="#2e2e2e",command=search)
Search_Button.place(x=10,y=200)
ElectricPanel=customtkinter.CTkFrame(master=app,width=200,height=600,fg_color="#451411")
ElectricPanel.place(x=320,y=50)
M1_Panel=customtkinter.CTkFrame(master=app,width=200,height=600,fg_color="#383838")
M1_Panel.place(x=550,y=50)
M2_Panel=customtkinter.CTkFrame(master=app,width=200,height=600,fg_color="#383838")
M2_Panel.place(x=780,y=50)
M3_Panel=customtkinter.CTkFrame(master=app,width=200,height=600,fg_color="#383838")
M3_Panel.place(x=1010,y=50)
ElectricText=customtkinter.CTkLabel(master=app,text="Panel Electrical Tools",font=FontMed)
ElectricText.place(x=320,y=20)
ElectricText=customtkinter.CTkLabel(master=app,text="Panel Mechanical Tools",font=FontMed)
ElectricText.place(x=550,y=20)
ElectricText=customtkinter.CTkLabel(master=app,text="Panel Mechanical Tools",font=FontMed)
ElectricText.place(x=780,y=20)
ElectricText=customtkinter.CTkLabel(master=app,text="Panel Mechanical Tools",font=FontMed)
ElectricText.place(x=1010,y=20)
SearchText= customtkinter.CTkLabel(master=app,text="Search:",font= FontMed)
SearchText.place(x=15,y = 150)
IDText=customtkinter.CTkLabel(app,text="ID:",font=FontMed)
IDText.place(x=15,y=500)
PLText=customtkinter.CTkLabel(app,text="Location:",font=FontMed)
PLText.place(x=15,y=450)
Search_Entry = customtkinter.CTkEntry(app)
Search_Entry.place(x= 100, y=150)
Add_ID_Entry = customtkinter.CTkEntry(app)
Add_ID_Entry.place(x= 50, y=500)
Add_Location_Entry = customtkinter.CTkEntry(app)
Add_Location_Entry.place(x= 100, y=450)
app.mainloop()
The search button should search in the Excel file for the ID in the text box. If it is found, then a box saying success
should appear. But when I press the search button, nothing happens.
Your search function is incorrect. you basicly comparing a string value directly with xl.cell.read_only(sheet,column = ['A'] it doesn't make sense. You can try kind of search function in below.
def search():
file = xl.load_workbook('Data.xlsx')
sheet = file["Sheet1"]
search_res = Search_Entry.get()
for row in sheet.iter_rows(values_only=True):
if search_res in row:
YesText=customtkinter.CTkLabel(master=app,text="yes",font=FontMed)
return