You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Tkinter文本框的逗号分隔值转为Python列表并优化UI

问题修正方案

核心问题解决

1. 正确转换逗号分隔值为列表

你直接用list()转换输入字符串会把每个字符拆成列表元素,这是错误的。正确做法是用split(',')分割字符串,同时处理元素前后空格、过滤空值:

# 替换print_value函数内的列表转换逻辑
inp = value_entered.get(1.0, "end-1c")
# 分割后清理每个元素,过滤空字符串
list_value = [item.strip() for item in inp.split(',') if item.strip()]

2. 添加文本框与按钮的间距

在pack()方法中通过pady参数设置垂直间距,给文本框的pack加上pady=(0,10),即可让它和下方按钮保持10像素的距离。

完整修正代码

from tkinter import *
from tkinter import ttk
from tkinter.messagebox import showinfo
from tkinter.filedialog import askopenfile
from tkinter import messagebox
import requests
from tqdm import tqdm
import pandas as pd
import time
import re

win = Tk()
win.geometry('640x480')

# 设置窗口标题
win.title("Test Tab")
# 创建标签页容器
notebook = ttk.Notebook(win)

# 创建标签页框架
tab1 = ttk.Frame(notebook)
tab2 = ttk.Frame(notebook)
tab3 = ttk.Frame(notebook)
tab4 = ttk.Frame(notebook)
costcenter_value = StringVar()
# 添加标签页到容器
notebook.add(tab1, text="TAB1 Test")
notebook.add(tab2, text="TAB2 Test")
notebook.add(tab3, text="TAB3 Test")
notebook.add(tab4, text="TAB4 Test")

notebook.pack(expand=1, fill='both')


def print_value():
    inp = value_entered.get(1.0, "end-1c")
    # 正确转换为列表:分割、去空格、过滤空元素
    list_value = [item.strip() for item in inp.split(',') if item.strip()]
    # 展示处理后的结果
    lbl.config(text=f"Provided Input: {list_value}")
   
def Year_SelectionEvent(value):
    if value == '2024':
        print('Selected 2024')
    elif value == '2023':
       print('Selected 2023')

# 版本选择事件
def Version_SelectionEvent(value):
    if value == '1.0':
        print('Selected 1.0')
    elif value == '2.0':
       print('Selected 2.0')
  

Label(tab3, text="Enter the list value  Details in comma separated!", font=('Helvetica', 10, 'bold')).pack()
value_entered = Text(tab3,height = 3,width = 50)
# 添加文本框与下方按钮的垂直间距
value_entered.pack(pady=(0,10))
Button(tab3, text='Show the Entered list values', command=print_value,bg='black', fg='white').pack()
lbl = Label(tab3, text = "")
lbl.pack()
Label(tab3, text="Choose the Year!",font=('Helvetica', 10, 'bold')).pack()
OPTIONS=["2024", "2023"]
year_clicked = StringVar(win)
year_clicked.set(OPTIONS[0])
year_selected = OptionMenu(tab3, year_clicked, *OPTIONS,command = Year_SelectionEvent).pack()
Label(tab3, text="Choose the Version!",font=('Helvetica', 10, 'bold')).pack()
OPTIONS=["1.0", "2.0"]
version_clicked = StringVar(win)
version_clicked.set(OPTIONS[0])
version_selected = OptionMenu(tab3, version_clicked, *OPTIONS,command = Version_SelectionEvent).pack()       


def download():
    print('Hello')
    # 执行自定义操作

Button(tab3, text='Download', command=download,bg='black', fg='white').pack()


win.mainloop()

说明

  • 输入100,200,300会得到列表['100', '200', '300'],输入带空格的100, 200, 300也能正确处理
  • 文本框与按钮的间距通过pady=(0,10)实现,第一个值是上方间距,第二个是下方间距
  • 修正了原代码中dwonalod的拼写错误,以及注释中的//(Python使用#作为注释符号)

内容的提问来源于stack exchange,提问作者thangaraj1980

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 20:54:54