AttributeError:“str”对象没有属性“tk”

2024-09-24 02:18:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在另一个函数def scan_for_text():中使用scan4yellow,然后使用来自scan4yellow的结果并将其打印到标签上,但它一直说它没有在home_screen()函数中定义,即使它在scan_for_text():中定义为全局变量

    global scan4yellow
    ##reads in the specific docx you want
    document = docx.Document(r'C:/Users/devff/Documents/Prac2.docx')
    ##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
                    print(scan4yellow)


def home_screen():
    global home_screen
    global scan4yellow
    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)
#    l1 = Label(home_page, textvariable=scan4yellow).grid(row=3, column=1, padx=(10, 0), pady=(10, 0))
#    t1 = Text.insert(END, "1.0", scan4yellow).grid(row=3, column=1, padx=(10, 0), pady=(10, 0))
    l2 = Label(home_screen, text=scan4yellow) #<----- problem is here 'scan4yellow' is not defined
    #print(scan4yellow)```

Tags: thetextinhomeforistagbutton
1条回答
网友
1楼 · 发布于 2024-09-24 02:18:06

请注意,如果且仅当docx中存在黄色文本时,才将值定义为scan4yellow变量。当我在您的代码段中指定了scan\u-for-text函数中最外层的“for”循环外的变量时,就可以很好地打印值了。在

尝试使用调试代码

import pdb; pdb.set_trace()

就在scan4yellow赋值之前,然后向上直到命中。我很确定您错过了一些docx标记(或者您的输入文件不包含黄色文本)。在

或者根本不调用“扫描”文本。在这种情况下,全局变量保持未定义状态。在

^{pr2}$

相关问题 更多 >