【问题标题】:TKinter not inserting a string to Text widgetTKinter 没有将字符串插入到 Text 小部件
【发布时间】:2018-05-25 17:41:50
【问题描述】:

我提前为长代码道歉,如果我违反任何规则,因为这是我第一次在这个网站上提问。我对 Python 还很陌生,但我目前正在为一个学校项目编写一个程序,该程序从 OpenFEC API 请求数据,然后将其显示在我使用 TKinter 创建的 GUI 中。如果我打印到控制台,一切都会很好,但是一旦我尝试将我的数据插入到 Text 小部件中,我就会收到一个错误,并且不会显示所有数据。错误是“TypeError:必须是 str,而不是 NoneType”,我尝试使用 str(varName) 将变量转换为字符串,但不起作用。我已经在这个特定问题上工作了几天,并在互联网上搜索了可能的解决方案,但我没有找到任何对我有帮助的东西。我很感激我能得到的任何帮助。

import json
import urllib.request
import urllib.parse
import urllib.error
from tkinter import *
from tkinter import ttk

def inputValidation(year):
    # Ask the user for a year
    # year = yearEntry.get()
    # Input validation
    year = int(year)  # Converting input to integer for comparison purposes
    if year >= 2003 and year <= 2020:
        message = "Here are the election dates  for " + str(year) + ':' + '\n'
    else:
        message = "Please enter a valid year."
    # Clear out any existing text from entry window
    data.config(state=NORMAL)
    data.delete(0.0, END)
    # Set the data window
    data.insert(1.0, message)
    data.config(state=DISABLED)
    # Convert the year back to a string
    year = str(year)
    return year

# Function to get API data
def apiCall(event, year, pageNumber):
    """ Requests data from OpenFEC API by constructing a url using predetermined
     values. """
    apiKey = 'rdudHBjgS5srIohVWYyyUL64AOsqVfRkGZD4gvMU'
    perPage = '90'  # Number of items to print per page
    electionYear = year
    nullHide = 'true'
    nullOnly = 'true'
    sort = sortNum.get()
    if sort == 1:
        sort = '-election_state'
    if sort == 2:
        sort = '-election_date'
    if sort == 3:
        sort = 'office_sought'
    url = ('https://api.open.fec.gov/v1/election-dates/?per_page=' + perPage +
           '&api_key=' + apiKey +
           '&election_year=' + electionYear +
           '&page=' + pageNumber +
           '&sort_hide_null=' + nullHide +
           '&sort_null_only=' + nullOnly +
           '&sort=' + sort)
    uh = urllib.request.urlopen(url)
    data = uh.read().decode()
    js = json.loads(data)
    print(url)
    return js                         # We receive a dictionary with all the 
info requested

# Function to print the API info
# Provide a year between 2003 - 2020
def electionDates(event):
    year = yearEntry.get()          # User provided year
    year = inputValidation(year)
    pageNumber = '1'
    js = apiCall(event, year, pageNumber)  # Call the API by using the first function
    pages = js['pagination']['pages']
    print('TOTAL PAGES: ', pages)
    # print('TOTAL ITEMS: ', items)
    while int(pages) >= int(pageNumber):
        idx = 0
        totalItems = 0
        items = 0
        print('PAGE', pageNumber, 'OF', pages)
        for item in js['results']:
            state = js['results'][idx]['election_state']
            date = js['results'][idx]['election_date']
            electionType = js['results'][idx]['election_type_full']
            notes = js['results'][idx]['election_notes']
            office = js['results'][idx]['office_sought']
            # Changing initials from API to full office names
            if office == 'S':
                office = office.replace('S', 'Senate')  # Print out the full word instead of just the initial
            if office == 'H':
                office = office.replace('H', 'House of Representatives')  # Print out the full word, not the initial
            if office == 'P':
                office = office.replace('P', 'President')  # Print out the full word instead of just the initial
            idx = idx + 1  # idx allows us to iterate through each item in the dictionary

            # Displaying Data in Text Box
            data.config(state=NORMAL)
            data.insert(2.0, '' +
                    '\n' 'Date: ' + date +
                    '\n' 'State: ' + state +
                    '\n' 'Election Type: ' + electionType +
                    '\n' 'Office: ' + office +
                    '\n' 'Notes: ' + notes +
                    '\n', END)
            data.config(state=DISABLED)
            items = items + 1
        pageNumber = int(pageNumber) + 1
        pageNumber = str(pageNumber)
        js = apiCall(event, year, pageNumber)  # Re-call the API function to print the next page


# -------- GUI CODE --------
root = Tk()
root.title('InfoLection')
frame = Frame(root)
sortNum = IntVar()

""" Create label, button, entry and text widgets into our frame. """
# --- Create instruction label ---
yearLbl = ttk.Label(root, text='Enter Year: ')
yearLbl.grid(row=0, column=1, sticky=E)

# --- Create Entry Box ---
yearEntry = ttk.Entry(root)
yearEntry.grid(row=0, column=2, columnspan=1, sticky=W)
yearEntry.delete(0, END)
yearEntry.insert(0, '')

# --- Create Submit Button ---
submitBtn = ttk.Button(root, text='Submit')
submitBtn.bind('<Button-1>', electionDates)
submitBtn.grid(row=3, column=0, columnspan=5, sticky=NSEW)

# Menu Bar
menu = Menu(root)
root.config(menu=menu)
# Submenu
subMenu = Menu(menu)
menu.add_cascade(label='About', menu=subMenu)
subMenu.add_command(label="Information")
subMenu.add_command(label='Exit')

# --- Radio Buttons to Select Sorting Method ---
# Label
sortByRB = ttk.Label(root, text='Sort by:')
sortByRB.grid(row=1, column=0, sticky=E)
# Radio Buttons
stateSortRB = ttk.Radiobutton(root, text='State', value=1, variable=sortNum)    
# Sort by state
stateSortRB.grid(row=2, column=1, sticky=W)
dateSortRB = ttk.Radiobutton(root, text='Date', value=2, variable=sortNum)      
# Sort by date
dateSortRB.grid(row=2, column=2, sticky=W)
officeSortRB = ttk.Radiobutton(root, text='Office', value=3, 
variable=sortNum)  # Sort by Office
officeSortRB.grid(row=2, column=3, sticky=W)

# --- Text Widget To Display Data ---
data = Text(root, width=50, height=25, wrap=WORD)
data.grid(row=4, column=0, columnspan=4, sticky=NSEW)

# --- Scroll Bar ---
scroll = ttk.Scrollbar(root, command=data.yview)
data['yscrollcommand'] = scroll.set
scroll.grid(row=4, column=5, pady=3, sticky=NSEW)

# Window Icon

# --- Keep Window Open ---
root.mainloop()

【问题讨论】:

  • 总是放完整的Traceback,还有其他有用的信息
  • 如果您将该代码缩减为专注于文本小部件问题的minimal reproducible example,您更有可能获得帮助。请求获取和 JSON 的东西是无关紧要的。只需制作一个最小的 GUI 并提供一些导致相同错误的虚假数据(例如,在字符串列表中)。
  • NoneTye 表示你得到None。您是否找到了变量中没有的 WHY 和 WHEN?
  • 这是回溯:Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\bryan\Anaconda3\lib\tkinter\__init__.py", line 1699, in __call__ return self.func(*args) File "C:/Users/bryan/Downloads/Alt GUI (1.0).py", line 160, in electionDates '\n' 'Notes: ' + notes + TypeError: must be str, not NoneType
  • 代码在 Py3 上为我运行,没有任何错误。您是否编辑了代码以使错误消失?

标签: python text tkinter widget nonetype


【解决方案1】:

您正在尝试连接 "Notes: "notes,但有时 notes 为 None,并且您无法将 None 添加到字符串中。您可以手动转换:

'\n' 'Notes: ' + str(notes) +

...但是我认为利用 Python 的 str.format() 方法会更容易,该方法会自动将其参数转换为字符串(在格式字符串中没有任何说明的情况下):

        data.insert(2.0, ''
                '\n' 'Date: {}'
                '\n' 'State: {}'
                '\n' 'Election Type: {}'
                '\n' 'Office: {}'
                '\n' 'Notes: {}'
                '\n'.format(date, state, electionType, office, notes)
                , END)

... 或者您可以使用 f-strings,尽管这些仅在 Python 3.6 及更高版本中可用:

        data.insert(2.0, '' +
                '\n' f'Date: {date}'
                '\n' f'State: {state}'
                '\n' f'Election Type: {electionType}'
                '\n' f'Office: {office}'
                '\n' f'Notes: {notes}'
                '\n', END)

【讨论】:

  • f-strings 方法很有效。非常感谢凯文!
【解决方案2】:

看起来您的 notes 变量返回为无。您可以这样做以使您的代码更健壮且更易于调试:

        undefined_s = 'N/A'
        data.insert(2.0, '' +
                '\n' 'Date: ' + (date or undefined_s) +
                '\n' 'State: ' + (state or undefined_s) +
                '\n' 'Election Type: ' + (electionType or undefined_s) +
                '\n' 'Office: ' + (office or undefined_s) +
                '\n' 'Notes: ' + (notes or undefined_s) +
                '\n', END)

我发现了另一个错误并修复了它。

       '&sort=' + str(sort))
       #          ^^^ added the str()

现在代码运行了。您应该默认打开其中一个单选按钮。

我确实看到一些注释以未定义的形式返回,但这不是错误,我会说。这只是您正在抓取的页面上的信息的问题。而且我确实看到其他一些字段也以未定义的形式返回,但同样,这是您正在抓取的页面的功能。

【讨论】:

  • 错误消失了,但我的大部分数据也消失了。 GUI 只显示“日期”。这是一个开始,所以我会继续工作。谢谢!
  • 我认为你不能像这样链接条件表达式。它们的优先级低于加法的优先级。作为一个更简单的例子,考虑"a" if True else "b" + "c" if True else "d";您可能希望它返回“ac”,但实际上它只返回“a”。
  • @Kevin - 谢谢,凯文!这就是我赶时间的结果!我还发现了一个错字,我输入了“is”而不是“if”。现在一切都在代码中修复了。
  • 好的,现在看起来不错 :-) 获得此类行为的另一种有趣方法是编写 (notes or undefined_s),利用 or 倾向于计算其最左边的真实操作数。跨度>
  • 我也认为问题出在数据上,但@Kevin 的解决方案解决了这个问题!
猜你喜欢
  • 1970-01-01
  • 2016-12-09
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 2023-03-22
  • 2011-03-30
  • 2015-05-08
相关资源
最近更新 更多