#python-3.x #tkinter
#python-3.x #tkinter
Вопрос:
Заявление о проблеме: пользовательский ввод через поле 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()
Ответ №1:
поскольку вы вызываете функцию close_window, попробуйте это, я добавил функцию write_to . В любом случае я предлагаю ОО-подход.
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()