2017-07-28 109 views
1

因此,我正在編寫一個應用程序,它允許您存儲您提供給列表框的信息,該列表框還可用於在某處打印數據。 (創建一個txt並存儲它。)我也嘗試了以前用戶發佈的所有建議,但找不到我的案例。任何幫助讚賞。TypeError:在Tkinter中需要一個整數(獲得類型str)

出於某種原因,我在標題中看到了錯誤。

這裏是我的代碼:

from tkinter import* 
from os import open 

def addData(): 
    dataInsert = dataEntry.get() 
    itemList.insert(END, dataInsert.upper()) 
    dataEntry.delete(0, END) 

def deleteData(): 
    dataSelect = itemList.curselection() 
    itemList.delete(dataSelect) 

def clearData(): 
    itemList.delete(0, END) 

def printData(): 
    dataDirectory = filedialog.askdirectory() 
    f = open('items.txt', dataDirectory, 'ab+') 
    f.write(bytes('', itemList.get(), 'UTF-8')) 
    f.close() 

def rootExit(): 
    root.destroy() 

root = Tk() 
root.config(bg='gray79') 
root.title('Inventory Recording Systems') 
root.geometry('1300x800') 
root.resizable(width=False, height=False) 

mainLabel = Label(text='Inventory Recording Systems', 
        font=('comic sans ms', 20, 'bold'), 
        bg='gray79', 
        fg='black') 

mainLabel.place(x=360, y=10) 

f1 = Frame(root, 
      bg='black', 
      width=300, 
      height=40) 

f1.place(x=40, y=22) 

f2 = Frame(root, 
      bg='black', 
      width=300, 
      height=40) 

f2.place(x=950, y=22) 

dataLabel = Label(root, 
        text='Enter Data:', 
        font=('comic sans ms', 20, 'bold'), 
        bg='gray79') 

dataLabel.place(x=10, y=130) 

dataEntry = Entry(root, 
        font=('arial', 16, 'bold')) 

dataEntry.place(x=250, y=142) 

itemList = Listbox(root, 
        font=('arial', 15, 'bold'), 
        width=47, 
        height=16) 

itemList.place(x=10, y=200) 

addButton = Button(root, 
        text='Add Data', 
        font=('arial', 20, 'bold'), 
        bg='gray89', 
        fg='black', 
        relief=GROOVE, 
        width=15, 
        height=1, 
        bd=5, 
        command=addData) 

addButton.place(x=865, y=215) 

deleteButton = Button(root, 
        text='Delete Data', 
        font=('arial', 20, 'bold'), 
        bg='gray89', 
        fg='black', 
        relief=GROOVE, 
        width=15, 
        height=1, 
        bd=5, 
        command=deleteData) 

deleteButton.place(x=865, y=345) 

clearButton = Button(root, 
        text='Clear Data', 
        font=('arial', 20, 'bold'), 
        bg='gray89', 
        fg='black', 
        relief=GROOVE, 
        width=15, 
        height=1, 
        bd=5, 
        command=clearData) 

clearButton.place(x=865, y=470) 

printButton = Button(root, 
        text='Print Data', 
        font=('arial', 20, 'bold'), 
        bg='gray89', 
        fg='black', 
        relief=GROOVE, 
        width=15, 
        height=1, 
        bd=5, 
        command=printData) 

printButton.place(x=865, y=595) 

exitButton = Button(root, 
        text='Exit', 
        font=('arial', 10, 'bold'), 
        bg='gray89', 
        fg='black', 
        relief=GROOVE, 
        width=6, 
        height=1, 
        bd=5, 
        command=rootExit) 

exitButton.place(x=1212, y=752) 

root.mainloop() 
+0

請給我們完整的錯誤。還請包括足夠的代碼,以便我們可以重現您的錯誤,這給出了不同的錯誤 – jacoblaw

+0

TypeError:一個整數是必需的(獲得類型str) – Yoshix

+0

我的意思是像完整的Traceback,輸出的一切。並請提供代碼,以便它可以被複制 – jacoblaw

回答

0

,因爲你的打印功能,試圖以這種形式打開一個文件你收到這個錯誤。 os中的open函數按照該順序接受文件名,模式,緩衝區。這是你的腳本中的內容:

f = open('items.txt', dataDirectory, 'ab+') 

你正在傳遞(文件名,路徑,模式)。相反,你應該這樣做:

f = open(dataDirectory+'/items.txt', mode='a') 

順便說一句,你仍然有錯誤,你如何寫作。我建議你閱讀OS項目文檔和其在Python 3.轉到您的終端使用和運行這個:也

import os 
help(os) 

,請閱讀本article,它會幫助你從蟒蛇2遷移代碼3並將幫助您發現遷移的潛在問題。該段在你的情況特別有趣的是這個:

As part of this dichotomy you also need to be careful about opening files. Unless you have been working on Windows, there is a chance you have not always bothered to add the b mode when opening a binary file (e.g., rb for binary reading). Under Python 3, binary files and text files are clearly distinct and mutually incompatible; see the io module for details. Therefore, you must make a decision of whether a file will be used for binary access (allowing binary data to be read and/or written) or textual access (allowing text data to be read and/or written). You should also use io.open() for opening files instead of the built-in open() function as the io module is consistent from Python 2 to 3 while the built-in open() function is not (in Python 3 it’s actually io.open()). Do not bother with the outdated practice of using codecs.open() as that’s only necessary for keeping compatibility with Python 2.5.

+0

未找到必需的參數「標誌」。我嘗試爲這兩個分區創建一個獨立的函數,甚至創建一個現有的文件,但沒有任何作用。只是給出了一個沒有結束的錯誤序列。 – Yoshix

+0

正確。如前所述,您將不得不重構整個打印功能。它看起來像你試圖使用舊的2.7打開和寫入文件的方式。閱讀我提供的遷移文章並找出需要更新的內容。 – Dom

+0

好像我在那裏找不到我的問題。 :( 順便說一句,我從來沒有使用Python2,只是有時想出我自己的Python3函數哈哈。 – Yoshix

0

參數編碼具有這樣

此代碼需要重新編寫爲

f.write(bytes('', itemList.get(), 'UTF-8')) 

傳遞
f.write(bytes('', itemList.get(), encoding='UTF-8')) 
相關問題