繁体   English   中英

Python:在 Excel 文件中保存单选按钮 tkinter 值

[英]Python: Save radio Button tkinter values in an Excel file

我正在尝试创建一个带有 Tkinter 和 4 个单选按钮的小界面,每次我单击其中一个按钮时,它都会打印按钮的值,我已经设法做到了!

但是,我希望每次单击 tkinter window 中的一个按钮时,将打印值保存在 Excel 文件中。

下面是我使用的代码和我得到的结果:

from tkinter import *
import tkinter as tk

master = Tk()
master.geometry("175x175")
 
# Tkinter string variable
# able to store any string value
v = IntVar(master, "0") #0 pour qu'aucun des boutons soit sélectioné par défaut

# Dictionary to create multiple buttons
values = {"classe 1" : "1",
          "classe 2" : "2",
          "classe 3" : "3",
          "classe 4" : "4"}


def affiche ():
    val = v.get()
    print(val)
    
    data = [
       ['class1', val],
     
       ]


labelValue = tk.Label(master, 
         text="""Choose your class affectation:""",
         justify = tk.LEFT,
         padx = 20).pack()

for (text, value) in values.items():
    Radiobutton(master, text = text, variable = v, command = affiche, 
                value = value).pack(side =TOP, ipady = 5)

# Infinite loop can be terminated by
# keyboard or mouse interrupt
# or by any predefined function (destroy())
mainloop()

在下方捕获的右侧是每次单击时我希望保存在 excel 文件中的值。

我分别点击 class1, class2, class3 和 class4 得到它们enter image description here

我是初学者,任何帮助表示赞赏!

非常感谢你。

我刚刚完成了之前的程序

def affiche ():
    val = v.get()
    print(val)
    workbook = xlsxwriter.Workbook('Mon fichier.xlsx')   
    worksheet = workbook.add_worksheet("My sheet") 
    data = [
       ['class1', val],
       ['class2', val],
       ['class3', val],
       ['class4', val],
       ]
    
    caption = 'My data.'

    # Set the columns widths.
    worksheet.set_column('B:G', 12)

    # Write the caption.
    worksheet.write('B1', caption)

    # Add a table to the worksheet.
    worksheet.add_table('B4:L4', {'header_row': 0})

    # Table data can also be written separately, as an array or individual cells.
    worksheet.write_row('B4', data[0])
    worksheet.write_row('B5', data[1])
    worksheet.write_row('B6', data[2])
    worksheet.write_row('B7', data[3])
    workbook.close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM