PySimpleGUI表格:跳过列表的第一个元素时出现索引错误。

huangapple go评论59阅读模式
英文:

PySimpleGUI Table index out of error when I skip first element of list

问题

在上面的代码中,你有一个名为 calendar_dates 的列表,你尝试将其作为参数传递给 PySimpleGUI 的 Table 元素,并使用 [1:] 来跳过第一个元素。但是你遇到了 Column index 1 out of bounds 的错误。

如果你想跳过第一个元素,你应该使用 [1:] 而不是 [:1]。所以你的代码是正确的,但你可能需要检查其他地方是否有可能引起这个错误的问题。

英文:

I'm making a small program with PySimpleGUI and encountered a very weird problem that doesn't make any sense to me.

def open_view_window(date):

    calendar_dates = db_session.factory().query(Calendar.full_date).all()
    calendar_dates = [ e[0] for e in calendar_dates ]
    calendar_dates.sort(key=lambda date: datetime.strptime(date, "%d-%m-%Y"))
    calendar_dates.insert(0, "")
    print(calendar_dates)
    tag_names = db_session.factory().query(Tag.name).all()
    tag_names = [ e[0] for e in tag_names ]
    tag_names.insert(0, "")
    print(tag_names)

    if date in calendar_dates:
        default_val = date
    else:
        default_val = calendar_dates[1]

    view_layout = [[sg.Text('Filter by:', key='FILTER')],
                   [sg.Text('Date:'), sg.Combo(values=calendar_dates, default_value=default_val, readonly=True), sg.Text('Tag:'), sg.Combo(values=tag_names, default_value=tag_names[0], readonly=True)],
                   [sg.Table(values=calendar_dates[1:])]]

As you can see in the code above, I have a list calendar_dates which I create from the contents of my database. After I sort the list, I insert a new empty element at the first position of the list.
But when I try to pass the list as an argument to the PySimpleGUI Table element and skip the first element with [1:], as it's no use for my case, I get the following error:

_tkinter.TclError: Column index 1 out of bounds

As I was trying to find out what I was doing wrong, I changed the list to skip the last element instead of the first with [:1], and that worked for some reason.

Please help, what am I doing wrong?

答案1

得分: 1

将您的表格元素设置标题否则将出现异常
```python
_tkinter.TclError: 列索引 ? 超出界限

尝试删除与您问题无关的代码,在这里简化您的问题的代码。

import PySimpleGUI as sg

data = [['06/10/23'], ['06/11/23'], ['06/12/23'], ['06/13/23']]
calendar_dates = [date[0] for date in data]
data.insert(0, [""])

sg.set_options(font=("Courier New", 12))
layout = [
    [sg.Text('按日期筛选:', key='FILTER')],
    [sg.Text('日期:'), sg.Combo(values=calendar_dates)],
    [sg.Table(values=data[1:], headings=["日期"], auto_size_columns=False, col_widths=[10], expand_x=True, justification='center')],
]
sg.Window('演示', layout).read(close=True)
英文:

Set the headings for your Table element, or you will get exception

_tkinter.TclError: Column index ? out of bounds

Try to remove code not related to your question, reduced code for your question here.

import PySimpleGUI as sg

data = [['06/10/23'], ['06/11/23'], ['06/12/23'], ['06/13/23']]
calendar_dates = [date[0] for date in data]
data.insert(0, [""])

sg.set_options(font=("Courier New", 12))
layout = [
    [sg.Text('Filter by:', key='FILTER')],
    [sg.Text('Date:'), sg.Combo(values=calendar_dates)],
    [sg.Table(values=data[1:], headings=["Date"], auto_size_columns=False, col_widths=[10], expand_x=True, justification='center')],
]
sg.Window('Demo', layout).read(close=True)

huangapple
  • 本文由 发表于 2023年6月13日 17:50:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463665.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定