コードを実行しようとすると、TypeErrorが表示されます。 状態:TypeError: 'in'は、メソッドではなく、左のオペランドとして文字列を要求します。TypeError: 'in <string>'は、メソッドではなく、左オペランドとして文字列が必要です。PYTHON
正確にはどういう意味か、それを修正する方法がわかりません。
エラーがで作成されたコードは次のとおりです。
from tkinter import *
from tkinter import messagebox
def saveCustomer() :
ClientID = ClientIDVar.get()
ClientID = ClientID.ljust(50)
Surname = SurnameVar.get()
Surname = Surname.ljust(50)
Forename = ForenameVar.get()
Forename = Forename.ljust(50)
Age = AgeVar.get()
Age = Age.ljust(50)
Gender = GenderVar.get()
Gender = Gender.ljust(50)
#lengthcheck
ClientID = ClientIDVar.get()
if len(ClientID) > 5 :
messagebox.showerror("Error","Too many characters in ClientID")
return
Surname = SurnameVar.get()
if len(Surname) > 20 :
messagebox.showerror("Error","Too many characters in Surname")
return
Forename = ForenameVar.get()
if len(Forename) > 15 :
messagebox.showerror("Error","Too many characters in Forename")
return
Age = AgeVar.get()
if len(Age) > 2 :
messagebox.showerror("Error","Too many characters in Age")
return
Gender = GenderVar.get()
if len(Gender) > 2 :
messagebox.showerror("Error","Too many characters in Gender")
return
#presencecheck
if ClientID == "":
messagebox.showerror("Error","Please enter characters into ClientID")
return
if Surname == "":
messagebox.showerror("Error","Please enter characters into Surname")
return
if Forename == "":
messagebox.showerror("Error","Please enter characters into Forename")
return
if Age == "":
messagebox.showerror("Error","Please enter characters into Age")
return
if Gender == "":
messagebox.showerror("Error","Please enter characters into Gender")
return
#rangecheck
if (int(Age) < 18) or (int(Age) > 99):
messagebox.showerror("Error","Please enter an age between 18 and 99")
return
#typecheck
if not ClientID.isnumeric():
messagebox.showerror("Error","Please enter alphabetical characters and numerical values into ClientID")
return
if not Surname.isalpha():
messagebox.showerror("Error","Please enter alphabetical characters into Surname")
return
if not Forename.isalpha():
messagebox.showerror("Error","Please enter alphabetical characters into Forename")
return
if not Age.isnumeric():
messagebox.showerror("Error","Please enter numerical values into Age")
return
if not Gender.isalpha():
messagebox.showerror("Error","Please enter alphabetical characters and numerical values into Gender")
return
#open the file to append - if it's not there it'll be created
fileObject = open("Customers.txt","a")
# write to the file with a newline character at the end
fileObject.write(ClientID + Surname + Forename + Age + Gender + "\n")
fileObject.close()
return
def searchCustomer() :
Found = "N"
ClientID = ClientIDVar.get
# try opening the file for reading
try:
fileObject=open("Customers.txt","r")
# if it's not there then say
except IOError:
messagebox.showerror("Error","No file to read")
# if we did open it then let's carry on!
else:
while True:
recordVar=fileObject.readline()
# Python keeps reading till EOF then returns a blank
if recordVar=="":
fileObject.close()
break
if ClientID in recordVar[0:50] and not ClientID=="" :
Found = "T"
messagebox.showinfo("Information",str(RecordVar))
break
if Found == "F":
messagebox.showerror("Error","No Customer found.")
return
def makeWindow():
#declared my globals here as this is the 1st routine called
# the other routines have to be in front of this one as they get called by it
# and the parser would get upset if they weren't there
global ClientIDVar, SurnameVar, ForenameVar, AgeVar, GenderVar
#here's my window
win = Tk()
win.wm_title("FitnessCentre")
#split into two sections then further split into a grid
frame1=Frame(win)
frame1.pack()
Label(frame1, text="Fitness Leisure Centre", font=("Helvetica 12 bold")).grid(row=0, column=0)
Label(frame1, text="ClientID").grid(row=1, column=0, sticky=W)
ClientIDVar=StringVar()
title= Entry(frame1, textvariable=ClientIDVar)
title.grid(row=1,column=1,sticky=W)
Label(frame1, text="Surname").grid(row=2, column=0, sticky=W)
SurnameVar=StringVar()
genre= Entry(frame1, textvariable=SurnameVar)
genre.grid(row=2,column=1,sticky=W)
Label(frame1, text="Forename").grid(row=3, column=0, sticky=W)
ForenameVar=StringVar()
director= Entry(frame1, textvariable=ForenameVar)
director.grid(row=3,column=1,sticky=W)
Label(frame1, text="Age").grid(row=4, column=0, sticky=W)
AgeVar=StringVar()
leadactor= Entry(frame1, textvariable=AgeVar)
leadactor.grid(row=4,column=1,sticky=W)
Label(frame1, text="Gender").grid(row=5, column=0, sticky=W)
GenderVar=StringVar()
duration= Entry(frame1, textvariable=GenderVar)
duration.grid(row=5,column=1,sticky=W)
frame2 = Frame(win)
frame2.pack()
# build my buttons in the other frame then pack them side by side
b1= Button(frame2, text=" Save ", command=saveCustomer)
b2= Button(frame2, text=" Search ", command=searchCustomer)
b1.pack(side=LEFT); b2.pack(side=LEFT)
return win
#main program!
win = makeWindow()
win.mainloop()
私が受け取った完全なエラーメッセージがされた:
Exception in Tkinter callback
Traceback (most recent call last):
if ClientID in recordVar[0:50] and not ClientID=="" :
TypeError: 'in <string>' requires string as left operand, not method
あなたのコードは間違って書式設定されていますか?関数には 'def'がありますが、残りのコードは左詰めです。また、表示されている_exact_エラーを貼り付けてください。 – bedwyr
これはPythonで正しくフォーマットされているので、私はここでそれを更新して、Python上の動作を見ていきます。 –