我的Tkinter按钮命令只在

2024-10-03 06:27:01 发布

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

我正在为我的工作编写一个程序,其中我正在使用Smartsheet API。你知道吗

这是我的密码:

import smartsheet
import logging
from tkinter import *

token  = "API KEY HERE"
matt_workspace = 7813091611174788
ss_client = smartsheet.Smartsheet(token) #Matt's workspace  

ss_client.errors_as_exceptions(True)

#for gethering workspaces

workspace_response = ss_client.Workspaces.list_workspaces(include_all=True)
all_workspaces = workspace_response.data
workspaces_name_and_id= []
for x in all_workspaces:
  workspaces_name_and_id.append(([str(x.name)], x.id))

selected_workspaces = []

def raise_frame(frame):
frame.tkraise()

#TODO set up UI root
import tkinter as tk                # python 3
from tkinter import font  as tkfont # python 3

def set_selected_workspaces(listbox):
    for x in listbox.curselection():
        selected_workspaces.append(workspaces_name_and_id[x])
        return selected_workspaces

class WorkspacesPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        label = tk.Label(self, text="Workspace Selection", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)

        wkdirections = tk.Label(self, 
            text = "1. Press the 'gather' button to gather all your available workspaces. \n 2. Select the workspaces with sheets you want to edit from the list. (Holding ctrl selects individual, holding shift selects en masse)")
        wkdirections.pack(side=RIGHT, anchor=CENTER)

        WorkspaceList = Listbox(self, selectmode=EXTENDED, width=100, height=50)
        WorkspaceList.pack(side=LEFT)

        for x in workspaces_name_and_id:
            WorkspaceList.insert(END, (x))

        button = tk.Button(self, text="Next", command=lambda: [controller.show_frame("FoldersPage"), set_selected_workspaces(WorkspaceList) ,print(selected_workspaces)], padx=20, pady=3)
        button.place(relx=0.975, rely=0.975, anchor=SE)

我从我包含的代码中删除了两个不必要的类(太多了,请保持它比现有的少一些混乱(包括主App/Tk App))。你知道吗

我的问题是在我的“工作区选择”页面的“下一步”按钮中的“设置所选工作区”功能。你知道吗

现在,它正在用工作区及其各自的ID填充我的列表框。然后,当我单击“下一步”时,我希望它在列表框中的选定工作区中循环,并将其添加到我的列表“选定的\u工作区”中,以便稍后在我的程序中使用。你知道吗

问题是,当我点击它时,它只是在我的列表框中添加了第一个选择,从而使我相信它只是通过for循环中的第一个循环。你知道吗

有什么建议吗?请对我放轻松,我是一个新的体面的编码,我相信这是一个混乱。如果您需要任何澄清或更多的见解,请让我知道,我很困惑。你知道吗


Tags: andnamefromimportselfidforall
1条回答
网友
1楼 · 发布于 2024-10-03 06:27:01

我觉得你回来太早了。而不是这个

def set_selected_workspaces(listbox):
    for x in listbox.curselection():
        selected_workspaces.append(workspaces_name_and_id[x])
        return selected_workspaces

试试这个

def set_selected_workspaces(listbox):
    for x in listbox.curselection():
        selected_workspaces.append(workspaces_name_and_id[x])
    return selected_workspaces

您的循环将在第一次迭代时返回,我认为这不是您想要的。你知道吗

相关问题 更多 >