如何将Radiobutton的值附加到csv文件

时间:2018-01-08 14:42:39

标签: python csv tkinter

尝试在输入字段中打印内容之前附加radiobutton的值,但是能够将entry窗口小部件中的内容附加到csv文件。我很感激有关如何实现这一目标的任何帮助。

from tkinter import *
import csv


with open ("profile.csv", "w") as db:
    writer = csv.writer(db)
    writer.writerow(["NAME ", "GENDER"]) # create with heading


def save_details():
    global e1               # global variable to receive the data from entry
    data = e1.get()
    #Sex = R1.get
    # Sex = R2.get()
    totalinput = [ data] # Sex here to append to the csv file
    with open("profile.csv", "a") as savedb:
        w = csv.writer(savedb)
        w.writerow(totalinput)


def validate():
    value = option.get()  # this for radiobutton

    now = new.get()  # this for an entry widget

    if value != "male" and value != "female":
        print("An option must be selected")
    else:
        print(now)


root = Tk()
root.geometry("400x400")

new = StringVar()
e1 = Entry(root, textvariable=new)

option = StringVar()
R1 = Radiobutton(root, text="MALE", value="male", var=option, 
command=save_details, indicatoron=0)
R2 = Radiobutton(root, text="FEMALE", value="female", var=option, 
command=save_details, indicatoron=0)
button = Button(root, text="OK", command=validate)


e1.pack()
R1.pack()
R2.pack()
button.pack(side="bottom")

root.mainloop()

2 个答案:

答案 0 :(得分:1)

替换:

data = e1.get()

使用:

data = option.get()

Radiobutton分享一个基于Radiobutton s'的变量。声明变量更新其值。

如果选择optionvalue "male" R1"female",如果选择了R2option.get()"male",与"female"中一样,返回sudo apt install --reinstall php7.1 libapache2-mod-php7.1 sudo service apache2 restart 或{{1}}。

答案 1 :(得分:0)

为了完整性,请将save_details()validate功能绑定到OK按钮。然后将您的值添加到totalinput列表totalinput = [ data, Sex]以附加到from tkinter import * import csv with open ("profile.csv", "w") as db: writer = csv.writer(db) writer.writerow(["NAME ", "GENDER"]) # create with heading def save_details(): global e1 # global variable to receive the data from entry data = e1.get() Sex = option.get() # this will get the value of radio selected and append it to the file totalinput = [ data, Sex] # Sex here to append to the csv file with open("profile.csv", "a") as savedb: w = csv.writer(savedb) w.writerow(totalinput) def validate(): value = option.get() # this for radiobutton now = new.get() # this for an entry widget if value != "male" and value != "female": print("An option must be selected") else: print(now) root = Tk() root.geometry("400x400") new = StringVar() e1 = Entry(root, textvariable=new) option = StringVar() R1 = Radiobutton(root, text="MALE", value="male", var=option, indicatoron=0) R2 = Radiobutton(root, text="FEMALE", value="female", var=option, indicatoron=0) button = Button(root, text="OK", command=lambda :[save_details(), validate()]) e1.pack() R1.pack() R2.pack() button.pack(side="bottom") root.mainloop() 列表<Grid> <ThinkGeoClean:ListBoxCustom x:Name="listBoxCustom" /> </Grid> 文件。

Show()