Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:GUI中带有正则表达式的搜索函数需要全局变量,但未绑定的局部错误_Python_Regex_Variables_Search_Global - Fatal编程技术网

Python:GUI中带有正则表达式的搜索函数需要全局变量,但未绑定的局部错误

Python:GUI中带有正则表达式的搜索函数需要全局变量,但未绑定的局部错误,python,regex,variables,search,global,Python,Regex,Variables,Search,Global,这是我的第一个项目,我是一个完全的初学者,所以我道歉 我正在尝试创建一个带有GUI的库存系统。 当我遇到处理所有数据的问题时,我认为在学习SQLite之前,我应该尝试自己的糟糕数据组织方式,这样我就对该库的必要性有了更深的理解。目前,我已经被困在搜索功能上好几个星期了。我的数据“组织”在一个文本文件中,它由一个包含6个变量的类组成。变量存储到文本文件中,如下所示:;x1,x2,x3,x4,x5,x6 目前,我正在尝试使用正则表达式创建一个搜索函数,通过搜索x1、x2或x3可以找到整个类(x1-x

这是我的第一个项目,我是一个完全的初学者,所以我道歉

我正在尝试创建一个带有GUI的库存系统。 当我遇到处理所有数据的问题时,我认为在学习SQLite之前,我应该尝试自己的糟糕数据组织方式,这样我就对该库的必要性有了更深的理解。目前,我已经被困在搜索功能上好几个星期了。我的数据“组织”在一个文本文件中,它由一个包含6个变量的类组成。变量存储到文本文件中,如下所示:;x1,x2,x3,x4,x5,x6

目前,我正在尝试使用正则表达式创建一个搜索函数,通过搜索x1、x2或x3可以找到整个类(x1-x6)

我遇到的问题是,我的搜索值(x_search_nick(x1)、x_search_name(x2)或x_search_part(x3))都是由GUI中搜索窗口中的条目定义的。当我点击搜索按钮时,我希望它在一个单独的窗口中显示该搜索值的整个类属性。为了使它成为一个单独的窗口,它需要是一个单独的函数,但此时需要全局定义搜索变量。当将所有搜索变量设置为全局时,我会得到“UnboundLocalError:UnboundLocalError python local variable referenced before assignment”,因为如果使用x1、x2或x3进行搜索,其中两个变量将为空?无论哪种方式,我都尝试过用不同的方式来设置,但似乎找不到正确的方法

我对整个问题的措辞也很糟糕,所以我会把我的代码扔出去,希望有了这些信息和代码,问题会更容易被发现。除了代码之外,我还有一个文本文件Reagent_To_List.txt。这又是我的第一个项目,组织等都是一个残骸

多谢各位

from tkinter import *
import tkinter.messagebox
import re
#Tkinter Basics______________________________________________
root = Tk()
#Files to open_______________________________________________
Reagent_To_List_file = open("Reagent_To_List.txt","w")

topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)

#Class_____________________________________

class Reagent:
    def __init__(self, Nickname, Name, Part_Num, Lot_Num, Expiration,         
Manufacteur, ):
    self.Nickname = Nickname
    self.Name = Name
    self.Part_Num = Part_Num
    self.Lot_Num = Lot_Num
    self.Expiration = Expiration
    self.Manufacteur = Manufacteur

#Functions_________________________________

class New_Reagent_Button_1:

    def __init__(self, master):
        frame = Frame(master)
        frame.pack()

        self.New_Reagent_button =Button(topFrame, text="New Reagent",fg="Black", width=75, height=3, relief=RAISED, bd=18,command = self.new_Reg)
    self.New_Reagent_button.grid(row=0, column=0)



def new_Reg(self):
    newwin = Toplevel(root)
    newwin.geometry("450x500")

    Enter_Nick = Label(newwin, text = "Enter Nickname: ", bd = 25)
    Enter_Nick.grid(row=0, column=0)
    x = Entry(newwin, bd = 5, width=30)
    x.grid(row=0, column=1)

    Enter_Chem_Name = Label(newwin, text="Chemical Name: ", bd = 25)
    Enter_Chem_Name.grid(row=1, column=0)
    x_1 = Entry(newwin, bd=5, width=30)
    x_1.grid(row=1, column=1)

    Enter_Part = Label(newwin, text="Part Number: ", bd=25)
    Enter_Part.grid(row=2, column=0)
    x_2 = Entry(newwin, bd=5, width=30)
    x_2.grid(row=2, column=1)

    Enter_Lot = Label(newwin, text = "Lot Number: ", bd= 25)
    Enter_Lot.grid(row=3, column=0)
    x_3 = Entry(newwin, bd=5, width=30)
    x_3.grid(row=3, column=1)

    Enter_Exp = Label(newwin, text = "Expiration Date: ", bd= 25)
    Enter_Exp.grid(row=4, column=0)
    x_4 = Entry(newwin, bd=5, width=30)
    x_4.grid(row=4, column=1)

    Enter_Manu = Label(newwin, text = "Manufacteur: ", bd= 25)
    Enter_Manu.grid(row=5, column=0)
    x_5 = Entry(newwin, bd=5, width=30)
    x_5.grid(row=5, column=1)


    def Done_Save(x):
        input = x.get()
        input_1 = x_1.get()
        input_2 = x_2.get()
        input_3 = x_3.get()
        input_4 = x_4.get()
        input_5 = x_5.get()

        Reagent_To_List = (Reagent(x,x_1, x_2, x_3, x_4, x_5))
        Reagent_To_List_file.write(str(input + ", "))
        Reagent_To_List_file.write(str(input_1 + ", "))
        Reagent_To_List_file.write(str(input_2 + ", "))
        Reagent_To_List_file.write(str(input_3 + ", "))
        Reagent_To_List_file.write(str(input_4 + ", "))
        Reagent_To_List_file.write(str(input_5))
        Reagent_To_List_file.write("\n")
        Reagent_To_List_file.flush()

    Save_New_Reg = Button(newwin, text="Done/Save", bd=5, height=2, width= 8, command= lambda: Done_Save(x))
    Save_New_Reg.grid(row=6, column=0)

    Done_New_Reg = Button(newwin, text ="Exit", bd = 5, height = 2, width = 8, command = newwin.destroy)
    Done_New_Reg.grid(row = 6, column = 2)

    newwin.mainloop()


class Search_Button:

def __init__(self, master):
    frame = Frame(master)
    frame.pack()

    self.Search_button =Button(topFrame, text="Search For Reagent", fg="Black", width=75, height=3, relief=RAISED, bd=18, command = self.Search)
    self.Search_button.grid(row=1, column=0)

def Search(self):

    newwin = Toplevel(root)
    newwin.geometry("610x400")

    Enter_Search = Label(newwin, text="Enter\n Nickname: ", bd=25)
    Enter_Search.grid(row=0, column=0)
    global x_search_nick
    x_search_nick = Entry(newwin, bd=5, width=50)
    x_search_nick.grid(row=0, column=1)

    Enter_Search = Label(newwin, text="Enter\n Chemical Name: ", bd=25)
    Enter_Search.grid(row=3, column=0)
    global x_search_name
    x_search_name = Entry(newwin, bd=5, width=50)
    x_search_name.grid(row=3, column=1)

    Enter_Search = Label(newwin, text="Enter\n Part #: ", bd=25)
    Enter_Search.grid(row=6, column=0)
    global  x_search_part
    x_search_part = Entry(newwin, bd=5, width=50)
    x_search_part.grid(row=6, column=1)

    Done_New_Search = Button(newwin, text = "Exit", bd = 5, height = 2, width = 8, command = newwin.destroy)
    Done_New_Search.grid(row = 9, column = 2)

    B_Search = Button(newwin, text="Search", bd=5, height=2, width=8, command= self.Search_Function)
    B_Search.grid(row=9, column=0)






def Search_Function(self):

    newwin = Toplevel(root)
    newwin.geometry("550x700")

    Results_Header = Label(newwin, text="Search Results", font=("Times New Roman", 16), height=5, width=10)
    Results_Header.grid(row=0, column=1)
    Search_Results_List = Listbox(newwin, bd=3, height=20, width=50)
    Search_Results_List.grid(row=10, column=4)

        regex_nick = (x_search_nick.get()) + r"([a-zA-z0-9\-\/]+),\s([a-zA-z0-9\-\/]+),\s([a-zA-z0-9\-\/]+),\s([a-zA-z0-9\-\/]+),\s([a-zA-z0-9\-\/]+)"
    pattern_nick = re.compile(regex_nick)

    with open("Reagent_To_List.txt", "r") as f:
        contents = f.read()
        matches_nick = pattern_nick.finditer(contents)

        for match_nick in matches_nick:
            print(match_nick)
            return (match_nick)

            if match_nick.group(1) == x_search_nick.get():
                Search_Results_List.insert(1, x_search_nick.get() + ", " + match_nick.group(2) + ", " + match_nick.group(3) + ", " + match_nick.group(4) + ", " + match_nick.group(5) + ", " + match_nick.group(6))

    regex_name = (r"([a-zA-z0-9\-\/]+),\s" + x_search_name.get() + ",\s([a-zA-z0-9\-\/]+),\s([a-zA-z0-9\-\/]+),\s([a-zA-z0-9\-\/]+),\s([a-zA-z0-9\-\/]+)")
    pattern_name = re.compile(regex_name)

    with open("Reagent_To_List.txt", "r") as f:
        contents = f.read()
        matches_name = pattern_name.finditer(contents)

        for match_name in matches_name:
            print(match_name)
            return (match_name)

            if match_name.group(2) == x_search_name:
                Search_Results_List.insert(1, match_name.group(1) + ", " + x_search_name.get() + ", " + match_name.group(3) + ", " + match_name.group(4) + ", " + match_name.group(5) + ", " + match_name.group(6))

    regex_part = (r"([a-zA-z0-9\-\/]+),\s([a-zA-z0-9\-\/]+),\s" + (x_search_part.get()) + ",\s([a-zA-z0-9\-\/]+),\s([a-zA-z0-9\-\/]+),\s([a-zA-z0-9\-\/]+),\s([a-zA-z0-9\-\/]+)")
    pattern_part = re.compile(regex_part)

    with open("Reagent_To_List.txt", "r") as f:
        contents = f.read()
        matches_part = pattern_part.finditer(contents)

        for match_part in matches_part:
            print(match_part)
            return (match_part)

            if match_part.group(3) == x_search_part:
                Search_Results_List.insert(1, match_part.group(1) + ", " + match_part.group(2) + ", " + x_search_part.get() + ", " + match_part.group(4) + ", " + match_part.group(5) + ", " + match_part.group(6))

    scrollbar = Scrollbar(newwin, orient="vertical")
    scrollbar.config(command=Search_Results_List.yview)
    scrollbar.grid(row=10, column=5, sticky="ns")

    #except:
       # Error_Header = Label(newwin, text="Search Yielded No Results",  width = 30, height = 20, font= ("Times New Roman",20) )
        #Error_Header.grid(row=5, column=4)
        #print(error)


#Buttons____________________________________

New_b = New_Reagent_Button_1(root)
Search_b = Search_Button(root)

button_Edit_Reagent = Button(topFrame, text="Edit Reagent", fg="Black", width = 75, height = 3, relief = RAISED,bd= 18)
button_Delete_Reagent = Button(topFrame, text="Delete Reagent", fg="Red", width = 75, height = 3, relief = RAISED,bd= 18)
button_See_Reagent_List = Button(topFrame, text="See Reagent List", fg="Black", width = 75, height = 3, relief = RAISED,bd= 18)
button_Check_Expired = Button(topFrame, text="Check Expired", fg="Red", width = 75, height = 3, relief = RAISED,bd= 18)
button_Done = Button(topFrame, text="Done", fg="Red", width = 75, height = 3, relief = RAISED,bd= 18,command = root.destroy)


button_Edit_Reagent.grid(row=6, column=0)
button_Delete_Reagent.grid(row=7, column=0)
button_See_Reagent_List.grid(row=9, column=0)
button_Check_Expired.grid(row=11, column=0)
button_Done.grid(row=12, column=0)


root.mainloop()

在我看来,在
搜索功能
范围之外创建的
试剂_to_List.txt
文件导致了您的问题


如果在
Search\u函数(self,txt\u file)
中添加
txt\u文件
参数,并用
txt\u文件
替换函数中的所有试剂\u到\u List.txt,然后调用函数
Search\u函数(根,“试剂\u到\u List.txt”)
,它应该可以工作

在我看来,在
搜索功能
范围之外创建的
试剂列表.txt
文件导致了您的问题


如果在
Search\u函数(self,txt\u file)
中添加
txt\u文件
参数,并用
txt\u文件
替换函数中的所有试剂\u到\u List.txt,然后调用函数
Search\u函数(根,“试剂\u到\u List.txt”)
,它应该可以工作

你好,朋友,我很感谢你的回答和你花时间来看我的问题。当我实施您建议的更改时,我会导致“名称错误:名称‘搜索函数’未定义”。因此,我默认返回调用self.Search_函数(根,“试剂_to_List.txt”),但在尝试调用时,我得到了“TypeError:Search_函数()接受2个位置参数,但给出了3个位置参数”。我做的任何小改动似乎都无助于它需要两个参数。我也有点不明白为什么我的函数之外的试剂To_List.txt会产生这个问题。我还想补充一点,如果我以self.Search_函数(“试剂To_List.txt”)的形式调用该函数,结果是在打开def Search时,它会自动调用def Search_函数。我也意识到我糟糕的名字是如何引起解释问题的。我很高兴澄清或重新命名它会有帮助的地方。非常感谢。你好,朋友,我很感谢你的回答和你花时间来看我的问题。当我实施您建议的更改时,我会导致“名称错误:名称‘搜索函数’未定义”。因此,我默认返回调用self.Search_函数(根,“试剂_to_List.txt”),但在尝试调用时,我得到了“TypeError:Search_函数()接受2个位置参数,但给出了3个位置参数”。我做的任何小改动似乎都无助于它需要两个参数。我也有点不明白为什么我的函数之外的试剂To_List.txt会产生这个问题。我还想补充一点,如果我以self.Search_函数(“试剂To_List.txt”)的形式调用该函数,结果是在打开def Search时,它会自动调用def Search_函数。我也意识到我糟糕的名字是如何引起解释问题的。我很高兴澄清或重新命名它会有帮助的地方。非常感谢。