2017-07-28 15 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関数はfilename、mode、bufferの順で受け付けます。

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

パス(ファイル名、パス、モード)は、スクリプトに含まれています。代わりに、次のようなことを行う必要があります。

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

ところで、あなたは書いている方法にまだ誤りがあります。私はあなたがあなたの端末にOSのアイテムとPython 3ゴーでの使用のための文書を読むことをお勧めし、これを実行したい:また

import os 
help(os) 

を、このarticleを読んで、それはへのpython 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

必須の引数 'flags'が見つかりません。私はこれらの2つの機能を別々に作ってみましたが、既存のファイルを作成しても機能しませんでした。終わりのないエラーシーケンスを与えます。 – Yoshix

+0

正しい。前述のように、その全体の印刷機能をリファクタリングする必要があります。古い2.7のファイルを開く方法と書き込む方法を使用しようとしているようです。私が提供した移行記事を読んで、更新が必要なものを見つけてください。 – Dom

+0

そこに私の問題を見つけることができないようです。 :( Btw、私はPython2を一度も使用しませんでした。ちょうど時々私自身のPython3関数hahaを思いついています。 – Yoshix

0

パラメータの符号化は、このコードは

として

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

として再書き込みする必要があり、この

のように渡す必要があります

f.write(bytes('', itemList.get(), encoding='UTF-8')) 
関連する問題