簡體   English   中英

Python Tkinter Xlsxwriter嘗試將用戶輸入導出到Excel文件

[英]Python tkinter xlsxwriter trying to export user input to excel file

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()

問題是這行代碼:

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

e1e2是條目小部件。 它們的字符串表示形式(即:如果執行str(e1) )將是“一個奇怪的十進制數字”。

您需要調用小部件的get()方法來獲取內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM