如何在输入特定行的值时提取特定行的值 [英] How to extract a particular row value on inputting value of a particular row

查看:74
本文介绍了如何在输入特定行的值时提取特定行的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个输入列值的GUI程序 程序必须转到输入值等于该行值的行 并在文本框中显示特定的行值 但是我对如何做到这一点一无所知.

I want to make a GUI program that inputs the value of a column the program must go to the row where the inputted value is equal to the row value and display the particular row value in a text box but I am clueless on how to do this.

我尝试使用while循环来执行此操作,因此它将搜索整个excel文件,以检查输入数据的值是否等于文本框中的数据,但是输入不正确.

I tried using while loop to do it so it will search the whole excel file to check whether value of inputted data is equal to the data in the textbox but it did not go properly.

我正在使用蟒蛇使用python 3.7.0.

I am using python 3.7.0 using anaconda.

from tkinter import *
import openpyxl

a = Tk()
a.title('Return Book')
a.geometry('500x200')
heading = Label(a,text = 'Return Book')
heading.grid(row = 0,column = 1)
lab1 = Label(a,text = 'Enter Invoice Number:')
lab1.grid(row = 1, column = 0)
inv_field = Entry(a)
inv_field.grid(row = 1, column = 1)
inv_field.get()
find = Button(a,text = 'Find',width = 4,command =a.destroy)
find.grid(row = 2, column = 1)


def find():
    ##extradiction
    ##location of excel file
    path = "E:\Library Management\issue.xlsx"
    # workbook object is created 
    wb = openpyxl.load_workbook(path)   
    sheet = wb.active 
    max_col = sheet.max_column 

    # Will print a particular row value 
    for i in range(1, max_col + 1): 
    cell_obj = sheet.cell(row = 2, column = i) 
    print(cell_obj.value, end = " ")



a.mainloop()

我希望程序输入发票编号的值,并在整个数据库中搜索该编号,然后在文本框中打印该行.

I expect the program to input the value of the invoice number and search the whole database for the number and print the row in the textbox.

推荐答案

首先,您没有在代码中定义textbox来显示搜索结果.其次,在find按钮的command选项中使用a.destroy,应将其设置为find功能.另外,对于查找按钮和查找功能,您使用相同的名称find.

First you did not define the textbox in your code to show the search result. Second you use a.destroy in command option of find button, it should be set to find function. Also you use same name find for find button and find function.

建议添加Text小部件:

book_info = Text(a, width=40, height=5)
book_info.grid(...)

然后将find()函数重命名为find_book()并添加新的update_text()以显示搜索结果,如下所示:

Then rename find() function to find_book() and add a new update_text() to show the search result as below:

def update_text(info):
    book_info.delete(1.0, 'end')
    book_info.insert('end', info)

def find_book():
    inv_no = inv_field.get()
    if inv_no:
        wb = openpyxl.load_workbook('E:\Library Management\issues.xlsx')
        sheet = wb.active
        for row in sheet.rows:
            # assume invoice no is in column 1
            if row[0].value == inv_no:
                update_text('\n'.join(str(cell.value) if cell.value else '' for cell in row))
                return
        wb.close()
        update_text('Book not found')

最后更改find按钮的command选项以调用find_book()函数:

Finally change the command option of find button to call the find_book() function:

find = Button(a, text='Find', command=find_book)

这篇关于如何在输入特定行的值时提取特定行的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆