Tkinter 组合框显示从 CSV 导入到大括号中的项目 [英] Tkinter combobox showing items imported from CSV into curly brackets

查看:38
本文介绍了Tkinter 组合框显示从 CSV 导入到大括号中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮我弄清楚我应该在下面的代码中添加什么以从我的 tkinter 组合框中删除大括号而不删除我的城市名称中的空格.

例如.在我的组合框中,纽约显示为 {New York},而达拉斯显示为达拉斯.

我试图转换为一串列表而不是元组列表,但没有成功.

非常感谢.

世界

导入 tkinter 作为 tk从 tkinter 导入 ttk导入 csv# 创建 tkinter 窗口窗口 = tk.Tk()window.geometry('300x250')使用 open('C:/cities.csv', newline='') 作为 csvfile:spamreader = csv.reader(csvfile , delimiter=',')Tup1 = ()对于垃圾邮件阅读器中的行:Tup1 = [list(row)for row in spamreader]打印(Tup1)Combo = ttk.Combobox(窗口,宽度= 30,高度= 20)组合 .place(x = 50 , y = 50)组合 ['values']= Tup1window.mainloop()

请检查一下

当您读取 CSV 文件时,您会得到一个行列表.每行是一个列列表.由于您在这里有多行,因此您在垃圾邮件阅读器中收到嵌套列表,因此您使用组合框映射嵌套列表.使用扩展我已将城市添加到单独的列表中,从而消除了嵌套列表并使用组合框映射城市,从而消除了大括号

导入 tkinter 作为 tk从 tkinter 导入 ttk导入 csv# 创建 tkinter 窗口窗口 = tk.Tk()window.geometry('300x250')sp=[]使用 open('C:/cities.csv', newline='') 作为 csvfile:spamreader = csv.reader(csvfile, delimiter=',')对于垃圾邮件阅读器中的我:sp.extend(i)Tup1 = ()对于 sp 中的行:Tup1 = [sp 中的一行行]打印(Tup1)Combo = ttk.Combobox(窗口,宽度= 30,高度= 20)组合 .place(x = 50 , y = 50)组合 ['values']= Tup1window.mainloop()

基于评论

这里是csv格式

您可以使用组合框绑定根据从组合框中选择的值来填充另一个组合框中的值

导入 tkinter 作为 tk从 tkinter 导入 ttk导入 csv# 创建 tkinter 窗口窗口 = tk.Tk()window.geometry('300x250')状态=[]城市=[]使用 open(r'C:\cities.csv', newline='') 作为 csvfile:spamreader = csv.reader(csvfile, delimiter=',')下一个(垃圾邮件阅读器,无)对于垃圾邮件阅读器中的我:states.append(i[1])城市.append(i[2])Combo1 = ttk.Combobox(窗口,宽度= 30,高度= 20)Combo1 .place(x = 50 , y = 100)定义选择(事件):city_values=[]if(Combo.get() 在状态):city_values.append(cities[states.index(Combo.get())])Combo1 ['values']= city_valuesCombo = ttk.Combobox(窗口,宽度= 30,高度= 20)组合 .place(x = 50 , y = 50)组合 ['values']= 状态Combo.bind("<>",choose)window.mainloop()

输出

马哈拉施特拉邦-孟买新泽西-开普梅

编辑请根据您最后的评论检查片段.

states=[]城市=[]使用 open(r'cities.csv', newline='') 作为 csvfile:spamreader = csv.reader(csvfile, delimiter=',')下一个(垃圾邮件阅读器,无)对于垃圾邮件阅读器中的我:states.append(i[1])城市.append(i[2])stat_City=[[i,j] for i,j in zip(states,cities)]b = 列表()对于 stat_City 中的子列表:如果子列表不在 b 中:b.附加(子列表)打印(b)Combo1 = ttk.Combobox(窗口,宽度= 30,高度= 20)Combo1 .place(x = 50 , y = 100)定义选择(事件):city_values=[]对于 b 中的 i:if((Combo.get() == i[0]) and (Combo.get() not in city_values)):city_values.append(i[1])Combo1 ['values']= city_valuesc=列表()对于 b 中的 i:if(i[0] 不在 c):c.append(i[0])Combo = ttk.Combobox(窗口,宽度= 30,高度= 20)组合 .place(x = 50 , y = 50)组合 ['values']= cCombo.bind("<>",choose)

enter image description here

Can anyone help me to figure out what I should add to my code below to remove the curly bracket from my tkinter combobox without removing the space in the name of my cities.

Eg. New York showing up as {New York} while Dallas showing up as Dallas in my combobox.

I tried to convert to a string of lists instead of list of tuples but no luck.

Thank you so much.

WW

import tkinter as tk 
from tkinter import ttk
import csv

# Creating tkinter window 
window = tk.Tk() 
window.geometry('300x250') 


with open('C:/cities.csv', newline='') as csvfile:
  spamreader = csv.reader(csvfile , delimiter=',')
  Tup1 = ()
  for row in spamreader:
      Tup1 = [list(row)for row in spamreader]
      print(Tup1)

 
Combo = ttk.Combobox(window, width = 30, height = 20)
Combo .place(x = 50 , y = 50)
Combo ['values']= Tup1

window.mainloop() 

Please check this out

When you read a CSV file you get a list of rows. Each row is a list of columns. As you have multiple rows here you are recieving nested list in spamreader and hence you were mapping nested list with combobox. Using extend I have added cities to separate list thus eliminating the nested list and mapping cities with combobox thus eliminating the curly brackets

import tkinter as tk 
from tkinter import ttk
import csv

# Creating tkinter window 
window = tk.Tk() 
window.geometry('300x250') 

sp=[]
with open('C:/cities.csv', newline='') as csvfile:
  spamreader = csv.reader(csvfile, delimiter=',')  
  for i in spamreader:
      sp.extend(i)
Tup1 = ()
for row in sp:
    Tup1 = [row for row in sp ]
print(Tup1)

 
Combo = ttk.Combobox(window, width = 30, height = 20)
Combo .place(x = 50 , y = 50)
Combo ['values']= Tup1

window.mainloop() 

Edit: Based on comment

Here is the csv format

You can use combobox binding to populate value in another combobox based on selection of value from a combobox

import tkinter as tk 
from tkinter import ttk
import csv

# Creating tkinter window 
window = tk.Tk() 
window.geometry('300x250') 

states=[]
cities=[]
with open(r'C:\cities.csv', newline='') as csvfile:
  spamreader = csv.reader(csvfile, delimiter=',')
  next(spamreader,None)
  for i in spamreader:
      states.append(i[1])
      cities.append(i[2])
      
Combo1 = ttk.Combobox(window, width = 30, height = 20)
Combo1 .place(x = 50 , y = 100)
def choose(event):
    city_values=[]
    if(Combo.get() in states):
        city_values.append(cities[states.index(Combo.get())])
    Combo1 ['values']= city_values
 
Combo = ttk.Combobox(window, width = 30, height = 20)
Combo .place(x = 50 , y = 50)
Combo ['values']= states
Combo.bind("<<ComboboxSelected>>",choose)

window.mainloop() 

Output

Maharashtra-Mumbai
New jersey- Cape May

Edit Please check snippet based on your last comment.

states=[]
cities=[]
with open(r'cities.csv', newline='') as csvfile:
  spamreader = csv.reader(csvfile, delimiter=',')
  next(spamreader,None)
  for i in spamreader:
      states.append(i[1])
      cities.append(i[2])

stat_City=[[i,j] for i,j in zip(states,cities)]
b = list()
for sublist in stat_City:
    if sublist not in b:
        b.append(sublist)
print(b)
Combo1 = ttk.Combobox(window, width = 30, height = 20)
Combo1 .place(x = 50 , y = 100)

def choose(event):
    city_values=[]
    for i in b:
        if((Combo.get() == i[0]) and (Combo.get() not in city_values)):
            city_values.append(i[1])
    Combo1 ['values']= city_values

c= list()
for i in b:
    if(i[0] not in c):
        c.append(i[0])
Combo = ttk.Combobox(window, width = 30, height = 20)
Combo .place(x = 50 , y = 50)
Combo ['values']= c
Combo.bind("<<ComboboxSelected>>",choose)

这篇关于Tkinter 组合框显示从 CSV 导入到大括号中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆