Когда вы думаете, что «захватываете переменную», вы вызываете функцию. Один из вариантов - создать глобальную переменную 'f1' в getfile()
и scan4yellow
в scan_for_text()
. Нечто подобное должно работать.
#getfile method
def getfile():
global f1 # Make the global variable f1 here
f1 = filedialog.askopenfilename(filetypes=[("Word Files", "*.docx")]) #<-- selects word files
l2 = Label(home_screen, text=f1, bg="white").grid(row=1, column=1, sticky='W') #<-- should take the filepath selected in `f1` and show it
# admins screen after login
def home_page():
global home_screen
home_screen = Toplevel(login_screen)
home_screen.title("Home Page")
home_screen.geometry("800x600")
b1 = Button(home_screen, text="Select File", bg="white", command=getfile)
b1.grid(row=1, column=0, sticky='W')
l1 = Label(home_screen, bg="white", width="20")
l1.grid(row=2, column=0, sticky='W')
b2 = Button(home_screen, text="Scan File", width=8, bg="white", command=scan_for_text)
b2.grid(row=3, column=0, sticky='W')
b3 = Button(home_screen, text="Logout", width=8, bg="white", command=logout)
b3.grid(row=4, column=0, sticky='W')
b4 = Button(home_screen, text="Quit", width=8, bg="white", command=quit)
b4.grid(row=5, column=0, sticky='W')
def scan_for_text():
##reads in the specific docx you want
document = docx.Document(f1) # Change this to f1
##makes it so it is an element that is actually editable and usable
rs = document._element.xpath("//w:r")
##microsoft words schema so it knows what the xml is like and the parametres
WPML_URI = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
##bits and pieces to help find the highlighted pieces of text
##then leads onto if statements finding out the highlighted texts colour
tag_rPr = WPML_URI + 'rPr'
tag_highlight = WPML_URI + 'highlight'
tag_val = WPML_URI + 'val'
tag_t = WPML_URI + 't'
for word in rs:
for rPr in word.findall(tag_rPr):
high = rPr.findall(tag_highlight)
for hi in high:
if hi.attrib[tag_val] == 'yellow':
global scan4yellow # Make the global variable here
scan4yellow = (word.find(tag_t).text.encode('utf-8').lower())
l3 = Label(home_screen, text=scan4yellow, bg="white").grid(row=7, column=0, sticky='W')