Python tkinter xlsxwriter试图将用户输入导出到excel文件

时间:2016-02-11 03:23:15

标签: python user-interface tkinter

from tkinter import *
import sys,math,random,datetime,os,time
import tkinter.messagebox
from tkinter import filedialog
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showerror
from time import gmtime, strftime
import xlsxwriter

export = "Excel"
current_time = strftime("%m-%d-%Y %H:%M", gmtime())

root = Tk()
e1 = Entry()
e1.insert(10, "First Name")
e2 = Entry()
e2.insert(10, "Last Name")

这是我尝试将变量格式化为字符串的地方。

fullname = "%s %s" % (e1, e2)
title = ["Titan Tech", "Caleb Fahlgren made this!", "Python is life!", "FIRST Robotics!","Doesn't this make your life easier?"]
title = (random.choice(title))
root.title(title)
root.geometry("640x600")

#Submit Button
def Submit():
    submit = tkinter.messagebox.askquestion("Submit Entry", "Are you sure you want to submit?")
    if submit == "yes":

Xlsx计划。

        workbook = xlsxwriter.Workbook('TitanTechSummary.xlsx')
        worksheet = workbook.add_worksheet()
        bold = workbook.add_format({'bold': 1})
        worksheet.write('A1', 'Name:',bold)

这是我尝试为名字编写玩家输入的地方。或者tkinter Entry()。但是当我查看excel文档时,我会像一个奇怪的十进制数字一样。我一直在B1领域得到这个'.140041879004720 .140041884602944'

        worksheet.write_string('B1',fullname, bold)
        worksheet.write('C1', 'Date:',bold)

我也想对日期做同样的事情。

        worksheet.write('D1', 'Date')


        workbook.close()
        userconfirm = tkinter.messagebox.showinfo("Save","Your entry has been saved to an " + export + " document!")


def keypress(event):
    if event.keysym == 'Escape':
        root.destroy()


def Quit():
    quitask = tkinter.messagebox.askquestion("Quit", "Are you sure you want to quit?")
    if quitask == "yes":
            root.destroy()
def Insert():
    filen = askopenfilename()
    filen1 = tkinter.messagebox.showinfo("Saved", "If you opened a picture we saved it!")

firstname = Label(root, text="First Name",font=("Helvetica", 12),fg="green")
lastname = Label(root, text="Last Name",font=("Helvetica", 12),fg="green")
time = Label(root, text=current_time, font=("Helvetica", 12),fg="black")

TextArea = Text()
ScrollBar = Scrollbar(root)
ScrollBar.config(command=TextArea.yview)
TextArea.config(yscrollcommand=ScrollBar.set)
ScrollBar.pack(side=RIGHT, fill=Y)

Submit = Button(root, fg="white", bg="green", text="Submit", width=50, command=Submit, activebackground="yellow")
Quit = Button(root, fg="white", bg="green", text="Quit", width=50, command=Quit,activebackground="yellow")
Insert = Button(root,fg="white", bg="green", text="Insert Images", width=50, command=Insert,activebackground="yellow")

root.bind_all('<Key>', keypress)
firstname.pack()
e1.pack()
lastname.pack()
e2.pack()
time.pack()
TextArea.pack(expand=YES, fill=BOTH)
Insert.pack()
Submit.pack()
Quit.pack() 

mainloop()

1 个答案:

答案 0 :(得分:0)

问题在于这行代码:

fullname = "%s %s" % (e1, e2)

e1e2是条目小部件。他们的字符串表示(即:如果你做str(e1))将是&#34;一个奇怪的十进制数&#34;。

您需要调用窗口小部件的get()方法来获取内容:

fullname = "%s %s" % (e1.get(), e2.get())