Tkinter组合框,显示从CSV导入到花括号中的项目

2024-04-19 18:55:10 发布

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

enter image description hereenter image description here

enter image description here有人能帮我弄清楚我应该在下面的代码中添加什么,以便在不删除城市名称中的空格的情况下,从tkinter组合框中删除花括号

纽约以{纽约}的身份出现,而达拉斯则以达拉斯的身份出现在我的组合框中

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

非常感谢你

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() 

enter image description here


Tags: csvimageimport列表heretkinteras身份
1条回答
网友
1楼 · 发布于 2024-04-19 18:55:10

Combobox

请看这个

当您读取CSV文件时,会得到一个行列表。每行都是列的列表。因为这里有多行,所以您在spamreader中接收嵌套列表,因此您使用combobox映射嵌套列表。使用extend,我将城市添加到单独的列表中,从而消除了嵌套列表,并使用combobox映射城市,从而消除了花括号

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() 

编辑:基于评论

这是csv格式

CSV

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

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() 

输出

Maharashtra-Mumbai
New jersey- Cape May

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

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)

相关问题 更多 >