小编Dav*_*ews的帖子

Python 如何摆脱 PyDictionary 错误消息

我有以下代码来检查字典中是否有单词。如果该单词不存在,则调用dictionary.meaning将返回None。问题是它还会发出错误消息“错误:发生以下错误:列表索引超出范围”。我做了一些研究,看来我可以使用 try:, except: 的组合,但无论我尝试什么,错误消息仍然会打印出来。这是一个显示问题的测试用例。如何使该代码工作而不显示索引错误?

代码:

    def is_word(word):
        from PyDictionary import PyDictionary
        dictionary=PyDictionary()
        rtn = (dictionary.meaning(word))
        if rtn == None:
           return(False)
        else:
           return (True)

    my_list = ["no", "act", "amp", "xibber", "xyz"]

    for word in my_list:
        result = is_word(word)
        if result == True:       
           print(word, "is in the dictionary")
        else:
           print(word, "is NOT in the dictionary")
Run Code Online (Sandbox Code Playgroud)

输出:

no is in the dictionary
act is in the dictionary
amp is in the dictionary
Error: The Following Error occured: list index out of range
xibber …
Run Code Online (Sandbox Code Playgroud)

python dictionary python-3.x

1
推荐指数
1
解决办法
3026
查看次数

无法在条目小部件上设置 focus()

当我尝试将焦点设置在条目小部件上时,我收到错误,

Traceback (most recent call last):
  File "C:/PythonPrograms/Tkinter/test_case.py", line 13, in <module>
    entSearch.focus()
AttributeError: 'NoneType' object has no attribute 'focus'
Run Code Online (Sandbox Code Playgroud)

通过在堆栈溢出上搜索此错误的其他出现情况,修复似乎是在单独的行上调用网格方法。

entSearch = Entry(main, textvariable = text, width = 50, font='arial 12')
entSearch = entSearch.grid(row = 0, column = 1, sticky=W)
Run Code Online (Sandbox Code Playgroud)

而不是

entSearch = Entry(main,
                  textvariable = text,
                  width = 50, 
                  font='arial 12').grid(row = 0, column = 1, sticky=W)
Run Code Online (Sandbox Code Playgroud)

不幸的是这个修复对我不起作用。

from tkinter import *

main = Tk()
main.title("Test Case")
main.geometry('750x750')
main.configure(background='ivory3')

text = StringVar()

entSearch = Entry(main, textvariable = text, …
Run Code Online (Sandbox Code Playgroud)

python tkinter python-3.x

1
推荐指数
1
解决办法
1385
查看次数

Python Tkinter 如何更新 for 循环中的文本小部件?

我正在尝试更新 for 循环内的滚动文本小部件中的文本。print 语句每次通过循环都会显示更新的文本,但在循环完成之前我在 Tk 窗口中看不到任何内容。然后我看到'('这是循环中的', 4, '次。')'。我从未见过显示 0 到 3。

from   tkinter import *
from   tkinter import scrolledtext
import time

main = Tk()
main.title("test_loop")
main.geometry('750x625')
main.configure(background='ivory3')


def show_msg():
    global texw
    textw = scrolledtext.ScrolledText(main,width=40,height=25)
    textw.grid(column=0, row=1,sticky=N+S+E+W)
    textw.config(background="light grey", foreground="black",
             font='arial 20 bold', wrap='word', relief="sunken", bd=5)
    for i in range(5):
        txt = "This is ", i, " times though the loop."
        txt = str(txt)
        print(txt)
        textw.delete('1.0', END)  # Delete any old text on the screen
        textw.update()            # Clear the screen.
        textw.insert(END, …
Run Code Online (Sandbox Code Playgroud)

tkinter python-3.x

0
推荐指数
1
解决办法
5216
查看次数

标签 统计

python-3.x ×3

python ×2

tkinter ×2

dictionary ×1