尝试从其他python函数获取变量以使用tkinter按钮命令

时间:2019-05-26 18:37:24

标签: python tkinter

尝试从其他函数中调用变量,而不是仅获取函数,而是运行该函数,除非我按下按钮让它需要变量,否则我不希望这样做。

尝试了很多类似创建全局变量的操作,但是这导致它们给我错误,因为即使定义它们也没有定义它们。

import docx


#getfile method

def getfile():
    f1 = filedialog.askopenfilename(filetypes=[("Word Files", "*.docx")]) #<-- selects word files
    return f1

# admins screen after login


def home_page():
    global home_screen
    f1=getfile()#<-- these functions are used instead of grabbing the variable
    scan4yellow=scan_for_text()#<-- these functions are used instead of grabbing the variable
    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)
    l1 = Label(home_screen, bg="white", width="20")
    b2 = Button(home_screen, text="Scan File", width=8, bg="white", command=scan_for_text)
    b3 = Button(home_screen, text="Logout", width=8, bg="white", command=logout)
    b4 = Button(home_screen, text="Quit", width=8, bg="white", command=quit)
    l2 = Label(home_screen, text=f1, bg="white") #<-- should take the filepath selected in `f1` and show it
    l3 = Label(home_screen, text=scan4yellow, bg="white")
    return l2


def scan_for_text():
    #scan4yellow = scan_for_text()
    ##reads in the specific docx you want
    document = docx.Document(l2) #<--- reads in the filepath selected
    ##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':
                    scan4yellow = (word.find(tag_t).text.encode('utf-8').lower())
                    #return scan4yellow```

1 个答案:

答案 0 :(得分:0)

在您认为自己在“抓取变量”的地方,您正在调用该函数。一种选择是在'getfile()中创建全局变量'f1',在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')