如何将选择的选项写入txt文件?

编程入门 行业动态 更新时间:2024-10-25 20:19:45
本文介绍了如何将选择的选项写入txt文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我目前的目标是将数据发送到一个 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 06:02:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390109.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   选项   文件   txt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!