Python刷新GUI以创建另一个输出
我对Python非常陌生。我正在尝试创建一个应用程序,从Smartsheet打印一个数字,然后将其删除。问题是我只能打印一次,一旦我再次点击“创建”按钮,它会给出错误消息。我相信当我再次点击“创建”按钮时,它会返回从工作表中删除的编号。谢谢大家!Python刷新GUI以创建另一个输出,python,tkinter,smartsheet-api,Python,Tkinter,Smartsheet Api,我对Python非常陌生。我正在尝试创建一个应用程序,从Smartsheet打印一个数字,然后将其删除。问题是我只能打印一次,一旦我再次点击“创建”按钮,它会给出错误消息。我相信当我再次点击“创建”按钮时,它会返回从工作表中删除的编号。谢谢大家! {"response": {"statusCode": 404, "reason": "Not Found", "content": {"detail": {"ids": [3462338204985220], "type": "row"}, "err
{"response": {"statusCode": 404, "reason": "Not Found", "content": {"detail": {"ids": [3462338204985220], "type": "row"}, "errorCode": 1006, "message": "Not Found", "refId": "hcuqkioxqz46"}}}
以下是存储一系列数字的工作表:
这是我的密码:
#Smartsheet client access token
smartsheet_client = smartsheet.Smartsheet('access token')
#Order Dashboard sheet ID
MySheet=smartsheet_client.Sheets.get_sheet(sheet_id)
def conceptnum():
n=1
for Myrow in MySheet.rows:
while n==1:
for Mycell in Myrow.cells:
row_ids=Myrow.id
label1['text']=int(Mycell.value)
label2['text']="Your concept number is created"
smartsheet_client.Sheets.delete_rows(
sheet_id, # sheet_id
row_ids) # row_ids
n=n-1
Height=100
Width=200
root=tk.Tk()
canvas=tk.Canvas(root, height=Height, width=Width)
canvas.pack()
frame=tk.Frame(root, bg="grey")
frame.place(relx=0.05, rely=0.3, relwidth=0.9, relheight=0.4)
button=tk.Button(root, text="Create",command=conceptnum)
button.pack(side='bottom')
label1=tk.Label(frame,font=15)
label1.place(relx=0.1, rely=0.1,relwidth=0.8, relheight=0.8)
label2=tk.Label(root)
label2.place(relwidth=1,relheight=0.2)
root.mainloop()
正如当前编写的那样,您的
conceptnum()
函数正在执行以下操作:
for each row in the sheet ->
for each cell in the current row ->
...
delete the current row
因此,如果工作表包含多个列,脚本将:
- 获取工作表的第一行
- 获取正在处理的行的第一个单元格的值
- 从工作表中删除该行
- 获取正在处理的行的第二个单元格的值
- 从工作表中删除行->此删除行请求(以及对正在处理的行中每个附加列/单元格的后续请求)将返回“未找到”错误--因为该行在工作表中不再存在--您在读取第一个单元格值后将其删除
每次运行
concept\u num
函数时,您都需要获取工作表,以便my\u sheet
反映工作表的当前内容(即不再包含以前运行函数时删除的行)。我已经相应地更新了上面的代码片段(即,在concept\u num
函数的顶部添加了get sheet调用)。我还在代码片段的末尾添加了代码(使用else
),以便在单击创建按钮时,如果工作表不再包含数字,则相应地更新标签。Hello Kim,在我测试后,它返回了相同的错误消息。与tkinter代码有关吗?非常感谢。很抱歉,我错过了每次运行concept\u num
函数时都需要重新获取工作表的内容。我已经相应地更新了我的答案——特别是,将get sheet调用my\u sheet=smartsheet\u client.Sheets.get\u sheet(sheet\u id)
移动到concept\u num
函数中。有关更多详细信息,请参阅我更新答案的编辑4/13/2020部分。
def concept_num():
# get sheet
my_sheet = smartsheet_client.Sheets.get_sheet(sheet_id)
# initialize row_id
row_id = None
for row in my_sheet.rows:
# get the ID of the current (first) row
row_id = row.id
# get the Cell object for the first cell of the current (first) row
cell = row.cells[0]
# set labels
label1['text'] = int(cell.value)
label2['text'] = 'Your concept number is created'
# exit the 'for' loop (so that only the first row is processed)
break
# delete the row that was just processed
if row_id != None:
smartsheet_client.Sheets.delete_rows(sheet_id, row_id)
else:
label1['text'] = 'n/a'
label2['text'] = 'Concept number not found.'