【问题标题】:Using a GUI to write to a CSV file cells使用 GUI 写入 CSV 文件单元格
【发布时间】:2021-07-18 18:11:41
【问题描述】:

我正在尝试制作一个将写入 CSV 文件的 GUI。我希望输入框中的任何内容都写入相应的单元格。

我想在 CSV 中查看的条目示例如下:

vlan 8
192.168.1.1 255.255.255.0
fa2/0/1

但是,当我打开 data1.csv 时,它写道:

PY_VAR0
PY_VAR1
PY_VAR2

这是我写的python代码:

import csv
from tkinter import *

root = Tk()
root.geometry('300x300')
root.title("Cisco-Oneport")
photo = PhotoImage(file = "Logo.PNG")
root.iconphoto(False, photo)

def info():
    list_of_lists = [[f'{vlan}',],
                    [f'{ip}',],
                    [f'{port}',]]

    with open('data1.csv', 'w', newline='') as f:
        writer = csv.writer(f, delimiter=';')
        for sublist in list_of_lists:
            writer.writerow(sublist)

entry1_text = Label(text = "Type Vlan Number * ",)
entry2_text = Label(text = "Type IP Number and Subnet * ",)
entry3_text = Label(text = "Type in Fast Port * ",)

entry1_text.place(x = 15, y = 30)
entry2_text.place(x = 15, y = 90)
entry3_text.place(x = 15, y = 150)

vlan = StringVar()
ip = StringVar()
port = StringVar()

vlan_entry = Entry(textvariable = vlan, width = "10")
ip_entry = Entry(textvariable = ip, width = "30")
port_entry = Entry(textvariable = port, width = "10")

vlan_entry.place(x = 15, y = 60)
ip_entry.place(x = 15, y = 120)
port_entry.place(x = 15, y = 180)


register = Button(root,text = "Run", width = "10", height = "2", command = info, bg = "lightgreen")
register.place(x = 15, y = 240)

root.mainloop()

如何获得所需的输入?我对轻量级 GUI 编码还很陌生。

【问题讨论】:

    标签: python database csv user-interface tkinter


    【解决方案1】:

    .csv 文件显示StringVars 的 tcl/tk 名称。使用 .get() 获取值。标签和条目是在没有包含对象的情况下创建的,所以我看到一个带有文本的 GUI,而另一个带有运行按钮的 GUI。

    回答可以在控制台中复制和运行代码的问题更容易。因此,最好删除图标之类的东西,这些东西使用了另一台 PC 上不存在的文件。

    import csv
    from tkinter import *
    
    root = Tk()
    root.geometry('300x300')
    root.title("Cisco-Oneport")
    
    # Removed icon as I cant access the file
    
    # Use .get() to get the values of the StringVars, not the names of the StringVars
    def info():
        list_of_lists = [[f'{vlan.get()}',],
                        [f'{ip.get()}',],
                        [f'{port.get()}',]]
    
        with open('data1.csv', 'w', newline='') as f:
            writer = csv.writer(f, delimiter=';')
            for sublist in list_of_lists:
                writer.writerow(sublist)
    
    # Add root to put the all the objects in the same GUI
    entry1_text = Label(root, text = "Type Vlan Number * ",)
    entry2_text = Label(root, text = "Type IP Number and Subnet * ",)
    entry3_text = Label(root, text = "Type in Fast Port * ",)
    
    entry1_text.place(x = 15, y = 30)
    entry2_text.place(x = 15, y = 90)
    entry3_text.place(x = 15, y = 150)
    
    vlan = StringVar()
    ip = StringVar()
    port = StringVar()
    
    # Add root to put the all the objects in the same GUI
    vlan_entry = Entry(root, textvariable = vlan, width = "10")
    ip_entry = Entry(root, textvariable = ip, width = "30")
    port_entry = Entry(root, textvariable = port, width = "10")
    
    vlan_entry.place(x = 15, y = 60)
    ip_entry.place(x = 15, y = 120)
    port_entry.place(x = 15, y = 180)
    
    # 
    register = Button(root,text = "Run", width = "10", height = "2", command = info, bg = "lightgreen")
    register.place(x = 15, y = 240)
    
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2013-01-11
      • 1970-01-01
      • 2021-01-03
      • 1970-01-01
      • 2013-09-21
      • 2011-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多