我不知道为什么搜索按钮不起作用

问题描述 投票:0回答:1

“我不知道为什么搜索按钮不起作用”

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

'我不知道为什么搜索按钮不起作用,搜索按钮应该在 Excel 中搜索文本框中的 id,然后如果找到,则应该出现一个表示成功的框,但是当我按下搜索按钮时什么也没有发生,这是整个代码,我有点新,我不太了解Python。

python tkinter openpyxl customtkinter
1个回答
0
投票

您的搜索功能不正确。您基本上直接将字符串值与 xl.cell.read_only(sheet,column = ['A'] 进行比较,这没有意义。您可以尝试下面的搜索功能。

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  
© www.soinside.com 2019 - 2024. All rights reserved.