2016-02-11 39 views
0
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") 

這裏是我試圖將變量格式化爲字符串的地方。Python tkinter xlsxwriter試圖將用戶輸入導出爲ex​​cel文件

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場」 0.140041879004720 0.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() 
+0

嘗試'全名= 「%s%S」 %(e1.get(),e2.get())相反。 – Shaung

回答

0

問題是這行代碼:

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

e1e2是入門的小部件。他們的字符串表示(即:如果你這樣做str(e1))將是「一個奇怪的十進制數字」。

您必須調用控件的get()方法來獲取內容:

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

@CalebFahlgren:沒有這樣的東西作爲一個字符串小部件。我不知道你在說什麼。 –

+0

我如何寫入現有的Excel文件而不覆蓋它。 – asdf