python-3.x - 将 tkinter 框输入写入 excel 单元格
问题描述
问题陈述:通过 tkinter 框的用户输入未写入 excel 表!
我的主要问题是 .get() 语句。无论输入类型如何,我只想将用户输入打印在 excel 上。
提前致谢。
from tkinter import *
# define this function to close the window after text submition
def close_window():
window.destroy()
#window dimentions
window = Tk()
window.title("My App")
window.geometry('350x200')
v = StringVar()
user_data = Entry(textvariable=v)
user_data.pack()
ans = v.get()
# I need this input on excel
f= open('sht.csv','w')
f.write(ans)
f.close()
button = Button(text="Submit", command = close_window)
button.pack()
window.mainloop()
解决方案
因为你调用了close_window函数,试试这个,我添加了函数write_to。无论如何,我建议采用 OO 方法。
from tkinter import *
# define this function to close the window after text submition
def close_window():
window.destroy()
#window dimentions
window = Tk()
window.title("My App")
window.geometry('350x200')
v = StringVar()
user_data = Entry(textvariable=v)
user_data.pack()
# I need this input on excel
def write_to():
ans = v.get()
f= open('sht.csv','w')
print(ans)
f.write(ans)
f.close()
button = Button(text="Submit", command = write_to)
button.pack()
window.mainloop()
推荐阅读
- python - 为什么 NLTK 的 PoS 标注器对单词中的每个字母进行标记,而不是对每个单词进行标记?
- javascript - 动态下载javascript并稍后执行
- eclipse - 行尾同居终端和 Eclipse Git
- android - 写入加速度计数据
- intellij-idea - 无法将 functx 库与 intellij idea 社区版一起使用
- c++ - 如何使 Qt 主窗口相互连接?
- css - 用于 css SelectorText 的正则表达式
- php - 是否有可能在 woocommerce 中复制商店页面?
- knockout.js - Durandal:在一个孩子中创建功能并从另一个孩子中消费
- java - 从 Java 8 迁移到 Java 9