如何将选择的选项写入txt文件? [英] How to write to txt file which options been selected?

查看:26
本文介绍了如何将选择的选项写入txt文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的目标是将数据发送到一个 txt 文件,其中的 3 个单选按钮中的每个选项都已被选中.

My current goal is to send data to a txt file of which out of the 3 radio buttons have been selected for each option.

这是我当前的单选按钮代码:

here is my current code of radio buttons:

import tkinter as tk

main = tk.Tk()

info = ["option 1",
        "option 2"
         ]

vars = []
for idx,i in enumerate(info):
    var = tk.IntVar(value=0)
    vars.append(var)
    lblOption = tk.Label(main,text=i)
    btnYes = tk.Radiobutton(main, text="Yes", variable=var, value=2)
    btnNo = tk.Radiobutton(main, text="No", variable=var, value=1)
    btnNa = tk.Radiobutton(main, text="N/A", variable=var,value=0)
    lblOption.grid(column=0,row=idx)
    btnYes.grid(column=1,row=idx)
    btnNo.grid(column=2,row=idx)
    btnNa.grid(column=3,row=idx)


main.mainloop()

接下来,我尝试将选定的单选按钮数据发送到 .txt 文件.这是我尝试过的:

Next, I am trying to send selected radiobuttons data to a .txt file. Here is what I have tried:

def send(tk):
    text_file = open("logfile.txt", "a")
    text_file.write(var1.get())
    text_file.close()


open = tk.Button(main, text="open", command = open).grid()

main.mainloop()

但这会返回一个错误代码:

but this returns me an error code:

    return self.func(*args)
TypeError: open() missing required argument 'file' (pos 1)

我哪里出错了?

推荐答案

查看您的按钮小部件内部,command = open.. 您基本上是在调用按钮,这就是显示错误的原因.

Look inside your button widget, command = open.. you're basically calling the button that's why the error is showing up.

将其更改为command = send,并从函数中取出'tk',因为您不需要它.

Change it to command = send, and also take out the 'tk' from function, as you don't need it.

这篇关于如何将选择的选项写入txt文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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